Avoiding a memory leak
-
I have a function that returns a char* and would like to know which way to handle this as to avoid a leak. If the function were in this format char* Function(void) { char *szString = new char[50]; return szString; } And was called like so: char *szBuffer = Function(); would deleting szBuffer like so: delete szBuffer effectively erase the allocated memory?
-
I have a function that returns a char* and would like to know which way to handle this as to avoid a leak. If the function were in this format char* Function(void) { char *szString = new char[50]; return szString; } And was called like so: char *szBuffer = Function(); would deleting szBuffer like so: delete szBuffer effectively erase the allocated memory?
You need to call delete this way
delete [] szBuffer;
Sonork 100.41263:Anthony_Yio -
You need to call delete this way
delete [] szBuffer;
Sonork 100.41263:Anthony_Yio -
You need to call delete this way
delete [] szBuffer;
Sonork 100.41263:Anthony_YioThanks for the reply but a subquestion. Aren't the brackets supposed to be placed after delete only in case of multidimensional array? and if not, does this mean I have memory leaks all over my code when I have used new and delete directly? (...oh god no!!!) eg. char *szString = new char[20]; delete szString Or does this apply solely because it was returned from a function?
-
Thanks for the reply but a subquestion. Aren't the brackets supposed to be placed after delete only in case of multidimensional array? and if not, does this mean I have memory leaks all over my code when I have used new and delete directly? (...oh god no!!!) eg. char *szString = new char[20]; delete szString Or does this apply solely because it was returned from a function?
hmmm, I just realized that ommitting the brackets only delets the first element...I have a lot of search/replacing to do!!!! Thanks for the help
-
Thanks for the reply but a subquestion. Aren't the brackets supposed to be placed after delete only in case of multidimensional array? and if not, does this mean I have memory leaks all over my code when I have used new and delete directly? (...oh god no!!!) eg. char *szString = new char[20]; delete szString Or does this apply solely because it was returned from a function?
For multidimensional array like below
int *pSomething = new int [2][2];
the clean up codes will bedelete [] [] pSomething;
I am not sure ifchar *szString = new char[20]; delete szString
\ [MODIFIED] will cause a memory leak to your program [/MODIFIED] Sonork 100.41263:Anthony_Yio -
hmmm, I just realized that ommitting the brackets only delets the first element...I have a lot of search/replacing to do!!!! Thanks for the help
Actualy ommitting the brackets does free the memory, but only calls that destructor for the first object in the array. The brackets tells delete to call the destructor of every item in the array. Therefor, for basic types you could get away with not using the brackets (not recommened). If you are creating an array of objects that also allocate memory for there own use, then you must use the brackets so that there destructors will be called inorder for them to clean up after themselfs (a.k.a. free any memory they allocated). INTP
-
I have a function that returns a char* and would like to know which way to handle this as to avoid a leak. If the function were in this format char* Function(void) { char *szString = new char[50]; return szString; } And was called like so: char *szBuffer = Function(); would deleting szBuffer like so: delete szBuffer effectively erase the allocated memory?