Skip to main content
gamedev.net
🔒 Locked

Deleting returned pointers

Started by HappyDude Nov 30, 2001 at 8:45 PM 0 replies 300+ views
Original Post
HappyDude
HappyDude
I''m moderately sure I have a memory leak in one of my functions, but I''m completely out of ideas on how to fix it... My function returns a pointer a string it just created with new... the return value of the function is immediately passed as a parameter to another function in my main loop... char * mrexample(int length) { char * somestring=new char[length]; //This is never deleted sprintf(somestring,"Blahblah"); //Putting something there... return somestring; //Returning it... } //somewhere else in the program MessageBox(0,mrexample(5),mrexample(5),MB_OK); //Program ends This is just a simplified version because I doubt anyone wants to wade through my code... The memory that was allocated in mrexample never gets deleted... =/
Prosper/LOADED
Prosper/LOADED
Well... you didn''t actually ask any question but I think you want to do something like this :

char * tmp = mrexample(5);
MessageBox( NULL, tmp, tmp, MB_OK);
delete [] tmp;

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.