Thread: Returning char *

  1. #1
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466

    Returning char *

    This is about the string class function that returns a char * (null-terminated).

    char *MYSTRING::c_str();

    I thought it would be easy but I am having a few roadblocks:

    1) If I declare a local array and set it up correctly, I can't return a reference or a pointer to it. Also, since the size is variable, I can't create an array in the first place.

    2) Only thing left is dynamic memory allocation. However, that leads to a memory leak.

    So... how else can I return a null-terminated c string? The guts of my class use a char array but it isn't null-terminated so I can't return that...

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You should be returning a const char*, not a char*.

    I would null terminate your char array inside the class. You could also just always allocate space for at least one extra character, and then only add the null to the end only when they call c_str().

  3. #3
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    That would an easy way to do it. Unfortunately I'd have to go back and change my class invariant and probably a bunch of other things too.

    That is good advice though, I really don't think there is any other more straightforward way to do it...

    BTW, anyone know how std::string does it?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> BTW, anyone know how std::string does it?
    That's where I got the ideas. The libraries I've looked at either always append the null or just leave space for it and append it when necessary.

    You shouldn't have to change that much if you only set the null when c_str() is called. You have to write c_str() obviously, but you only have to change your allocation functions to always just add one to the final allocation value.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    std::string can do it whatever way it likes, but I haven't heard of any implementation that doesn't do it Daved's way.

    Another option would be to have another pointer member in the string class for the c_str() return value, which you delete[] at the next modification. This is valid behaviour for a basic_string-like class because the return value of c_str() is only guaranteed to be valid until the next modification. However, it requires you to think about what operations count as modifications. Which is a useful exercise in itself, but perhaps not what you want to do right now.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. returning char arrays!!!!
    By bobthebullet990 in forum C Programming
    Replies: 2
    Last Post: 03-30-2006, 07:05 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM