Solved

Proper deletion of wchar memroy in C++

Posted on 2010-08-20
3
386 Views
Last Modified: 2012-06-27
Team,

In my program function, I am creating three wchar_t arrays posted below and deleteing them at the end of the function before the function ends, but even after deletion it does not release the memory and continues to eat it up. Can someone tell me what I am doing wrong:

Creation:

wchar_t* char1 = new wchar_t[5000];
wchar_t* char2 = new wchar_t[5000];
wchar_t* char3 = new wchar_t[5000];

Deletion:
char1 = NULL;
char2 = NULL;
char3= NULL;
delete [] char1;
delete [] char2;
delete [] char3;
0
Comment
Question by:tr57285
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 32

Expert Comment

by:phoffric
ID: 33487868
Remove these 3 lines:
char1 = NULL;
char2 = NULL;
char3= NULL;
0
 
LVL 32

Accepted Solution

by:
phoffric earned 500 total points
ID: 33487891
You want to delete the memory region associated with the new.

wchar_t* char1 = new wchar_t[5000];
delete [] char1;

If the delete operator gets a NULL value to operate on, then it will do nothing. But in the above two lines, the delete operator gets the actual address associated with the new operator, and so will be able to properly deallocate the space created by the new operator.
0
 

Author Closing Comment

by:tr57285
ID: 33487957
Awesome!

Thanks
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question