Thread: Best practice: string manipulation functions

  1. #1
    Registered User
    Join Date
    Sep 2004

    Best practice: string manipulation functions

    I am fairly new to C coding and have a question regarding the best practice when using functions to manipulate strings. Basically I would like to know should I write a function like:

    char *manipulate(char *str)
    static char outstr[1024];

    < Insert function code >

    return outstr;

    or is it better to write:

    char *manipulate(char *outstr, char *str)
    < Insert function code >

    return outstr;

    I was just curious to know if I should be using one method or the other as a best practice from point of view of performance and or style.

    Thanks in advance for any feedback.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    I suppose it depends on what you need to do with it. If you need to return the modified string, you'll need to allocate it dynamicly, or use an internal static buffer. If you pass a buffer to store the modified string in, you'll also want to pass the size of the buffer, so that you don't run merrily along and trash your memory as you walk off the end of it.

    But really, it depends on what you're actually doing with the string.

    Hope is the first step on the road to disappointment.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Oregon, USA
    It's really personal preference IMO. Each has its pros and cons.

    In the first style the calling function doesn't have to worry about declaring the buffer. However, you can't do something like printf("%s:%s\n", manipulate("foo"), manipulate("bar")); because the static buffer will get overwritten by the second call to manipulate().

    In the second style you can do the printf() type things, but the calling function has to declare the buffers used by manipulate(). Also, your second method is a little dangerous in that manipulate() has no idea how big outstr is so you might be risking a buffer overflow. I'd rewrite it as char *manipulate(char *outstr, int size, char *str) where size is the size of outstr if you decide to go that route.

    A third style would be to have manipulate() dynamically allocate memory for the buffer, but then the calling function has to worry about free()'ing that memory and I personally think it's best for a function to provide its own clean-up whenever possible.

    Being consistent I think is the most important part. You don't want half your functions to work one way and the other half to work the other.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Sep 2004
    The second way is far more intuitive, and easier to debug if you have problems. You should pass in the size of the buffer as well in your second function though, so you dont overwrite it by accident.

    While the first method is technically correct, you will start to have problems when the function is called repeatedly. Especially if it is called from different threads. Picture this:

    char *pstr1, *pstr2;
    pstr1 = manipulate();
    pstr2 = manipulate();
    // now pstr1 and pstr2 both point to the same location in memory.  If you make
    // a change to one, it will change the other.  This can cause you major headaches.

  5. #5
    Registered User
    Join Date
    Sep 2004


    Thanks for all of the help, I think it is clear that the second method is probably the method of choice to use.

    Thanks for the tip about passing the size inside the function call as well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Creating String Functions
    By stickman in forum C++ Programming
    Replies: 8
    Last Post: 04-30-2006, 05:26 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM