Solved

Proper deletion of wchar memroy in C++

Posted on 2010-08-20
3
373 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
  • 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now