CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2013
    Posts
    10

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    May 2013
    Posts
    10

    Re: noob question on cost char * returned by various functions

    Thankyou

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: noob question on cost char * returned by various functions

    Quote Originally Posted by bigc++ View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured