-
May 13th, 2013, 10:29 PM
#1
noob question on cost char * returned by various functions
What is the programmers responsibility with respect to const char * returned by various functions, like the C++ string class c_str() function which returns a const char * to an c style string array? In VC++ I cannot delete a const char * which holds a string literal. Take the following code for example:
Code:
void func() //a useless function with illustrative code
{
string s1("abcd");
string s2("efgh");
const char * cc1 = s1.c_str(); //c_str() returns a const char * c style string pointer
s2.c_str(); //this returns a const char *, which must be allocated on the heap right?
delete cc1; //produces run time error in Release mode in VC++
}
The problem with the above code snip is that space is allocated on the heap (or so I believe) for the const char *'s returned by the 2 calls to c_str(). The delete attempt fails and there is no opportunity to delete the space allocated by const char * because its not assigned to anything (however I see c_str() used this way extensively)
So, if I cannot delete a const char *, how does the memory get recovered? Perhaps the string objects s1 and s2 themselves have pointers to the items on the heap made by c_str() calls and they get deleted by the destructors of s1 and s2 when the function ends?
I am a noob so please forgive me if this question has a glaringly obvious answer. I tried searching for an answer in the forums but could not find a precise answer. Also I am using VC++ and it has a peculiar const char * policy which is why I post this thread in the VC++ section.
-
May 13th, 2013, 10:42 PM
#2
Re: noob question on cost char * returned by various functions
It is the std::string object's responsibility for managing the memory of that C-style string. You should not worry about it, except that if the contents of the string object might change, then you should copy the C-style string if you need it later.
-
May 13th, 2013, 10:46 PM
#3
Re: noob question on cost char * returned by various functions
-
May 14th, 2013, 05:30 AM
#4
Re: noob question on cost char * returned by various functions
Originally Posted by bigc++
The problem with the above code snip is that space is allocated on the heap (or so I believe) for the const char *'s returned by the 2 calls to c_str().
You do not know where that string is located, and it isn't your responsibility to know where it comes from. All you know is that a const char* is returned. For example, a std::string may have a small array that holds short strings (thus it is known as short string optimization). That is not allocated on the heap, so your call to delete would be totally wrong.
So, if I cannot delete a const char *, how does the memory get recovered? Perhaps the string objects s1 and s2 themselves have pointers to the items on the heap made by c_str() calls and they get deleted by the destructors of s1 and s2 when the function ends?
See previous paragraph. You don't know or should not need to know how a std::string or any other standard library component handles its internals.
Regards,
Paul McKenzie
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|