Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi all,

Suppose I'm trying to do something like this:

C++
void func(char*& foo)
{
     char* bar = new char[1000]; //allocating more space somewhere else
     //some stuff
     if (foo != null) delete [] foo; //clean up, if there's something here
     foo = bar;
}


That is, the code makes foo point someplace else (maybe the current location is too small). In doing that, it deletes foo's current location, as a part of good memory management. This is fine, if the user passes a char* allocated using new. But if they do something like
char* string = "text";
func(string);


then the delete operation will fail: string literals can't be deleted. How do I work around this? Is there a way to check for string literals? I'd love for my code to be able to support them. I believe the correct behavior in the case of a string literal is to just skip the delete statement, since literals are handled internally (...right?). But that still leaves the question of how to detect whether or not foo points to a literal.

Thoughts?
Posted
Updated 12-Dec-12 16:20pm
v2

The best solution would be if you don't delete this at all; because in simple word your compiler wont tell you what kind of pointer is this(by the way what compiler are you using?)

you better delete your foo variable as required outside the function, after you call the function. Its not a very good idea deleting function blindly

but if you want do delete the variable inside anyway, then you better add a flag in the function parameter list
 
Share this answer
 
Comments
wolf9 13-Dec-12 0:19am    
It's a good idea, but the whole reason for doing things this way was to allow the caller to not have more memory management to worry about after the function call than they had before- no new pointers. By the same logic, it would be nice if they didn't have to worry about what kind of string they're passing. I have very few guarantees about the eventual usage for this function.
wolf9 13-Dec-12 0:20am    
To answer your question, I'm on microsoft's c++ compiler, and gcc.
wolf9 13-Dec-12 0:28am    
Also, I can delete foo outside of the function, but that won't get rid of the original location in memory. By the time the function returns, that location is no longer accessible, since foo now points to wherever bar is pointing. Likewise, I can't delete foo ahead of time, if my function ever needs to make use of what's in there to begin with (like a text substitution function). So, the only time that that information can be deleted is during the function call.
Mohibur Rashid 13-Dec-12 0:40am    
You can delete foo after calling function. If memory management is a big issue then just don't pass char string, you can simply send string type object. If you are experienced programmer then you already should know why your steps are not valid.
Use a simple rule: a creator destroys. Everything else is unreliable and unportable.

—SA
 
Share this answer
 
Comments
Mohibur Rashid 13-Dec-12 2:11am    
agree with you
Sergey Alexandrovich Kryukov 13-Dec-12 12:19pm    
Thank you, Mohibur.
—SA
Jibesh 14-Dec-12 22:18pm    
nice Sergey!!
Sergey Alexandrovich Kryukov 14-Dec-12 22:55pm    
Thank you.
--SA
Uses std::string instead and avoid problems.

Memory will be manegd by the class and it will help a lot to make a reliable program particulary if you don't fully understand how memory allocation works.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900