Click here to Skip to main content
12,697,699 members (27,056 online)
Rate this:
Please Sign up or sign in to vote.
See more: C++
Hi all,

Suppose I'm trying to do something like this:

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";

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.

Posted 12-Dec-12 16:53pm
Updated 12-Dec-12 17:20pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Use a simple rule: a creator destroys. Everything else is unreliable and unportable.

Mohibur Rashid 13-Dec-12 2:11am
agree with you
Sergey Alexandrovich Kryukov 13-Dec-12 12:19pm
Thank you, Mohibur.
jibesh 14-Dec-12 22:18pm
nice Sergey!!
Sergey Alexandrovich Kryukov 14-Dec-12 22:55pm
Thank you.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 14 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100