Thread: How to return a string from a function

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    144

    How to return a string from a function

    Hi, everyone. I have a function that optionally produces up to 3 strings. Each string has a maximum length, which is unique to each string.

    I can do it like this:
    Code:
    void producestrings (char (*sz1)[a], char (*sz2)[b], char (*sz3)[c]);
    
    char sz1[a];
    char sz2[b];
    char sz3[c];
    
    producestrings (&sz1, &sz2, &sz3);
    in which case the function risks wasting some memory if the strings are not as long as they could be. Or I can do this:

    Code:
    bool producestrings ( char **sz1, char **sz2, char **sz3);
    
    char *sz1;
    char *sz2;
    char *sz3;
    
    producestrings(&sz1, &sz2, &sz3);
    in which case the function allocates memory with malloc(), which must be free()d by the calling function. This means manual memory handling and the risk of forgetting to free() the memory.

    Which is better?

    TIA,
    Richard

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by Richardcavell View Post
    Which is better?
    That's probably a subjective question. The first one will trade memory to save CPU cycles because it doesn't have to malloc(), but the other will save memory by deciding what it needs at run-time, but waste CPU cycles allocating that memory.

    If your function is going to be called lots of times, go for the first one. If it's going to be called to create huge strings (i.e. on a desktop machine you'd be measuring them in megabytes) go for the second one. If neither of these apply, go for whichever one's easiest for the programmer (this is personal preference but I'd go for the first one). If both apply then go for the one that's easiest for the programmer and see if this works in practice - if not, think about changing it.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If the maximum lengths are sufficiently small, or if the expected wastage of space is sufficiently small or otherwise tolerable, the former might be better, though it is more typical to pass a pointer to the first character rather than a pointer to the array itself.

    The latter, on the other hand, is more general.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Richardcavell View Post
    Which is better?
    If you want to program in C, ditch your fear of memory management or switch to a less hands on language

    Of course, part of the task of programming is sparing yourself needless effort, and the first version might be considered such if you are using it for a specific task where the length of the strings can be hard-coded. Since there are three of them, this sounds like it might be such a specific function. In terms of "wasted memory", if it is only a kB (1024 bytes) or 4, I would not worry too much unless you are using this many times, and always keeping the strings. Ie, if you use the function 100 times, but never have more than 3 or 6 strings in memory, big deal. If you use the function 100 times and keep all 300 strings, then those extra kBs add up.

    In any case, this prototype:

    Code:
    void producestrings (char (*sz1)[a], char (*sz2)[b], char (*sz3)[c]);
    Is silly verging on wrong. In both cases, you use the same pointer-to-a-pointer parameter. It does not matter if you are going to allocate memory inside the function, or whether it has already been allocated, you still just pass in a **char.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    144
    Quote Originally Posted by laserlight View Post
    it is more typical to pass a pointer to the first character rather than a pointer to the array itself.
    Notice, however, that the method I've described ensures that the array is exactly the right size.

    Richard

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it possible to return a string in a function?
    By buggerboy_3 in forum C Programming
    Replies: 7
    Last Post: 04-28-2007, 01:14 PM
  2. Replies: 6
    Last Post: 04-09-2006, 04:32 PM
  3. Return String from a function
    By muk_luk in forum C Programming
    Replies: 2
    Last Post: 03-21-2004, 12:14 PM
  4. Why won't this function return the new string value?
    By Jacquiline in forum C Programming
    Replies: 1
    Last Post: 04-06-2003, 08:45 AM
  5. Getting a function to return a string
    By Necrodeemer in forum C++ Programming
    Replies: 5
    Last Post: 01-17-2003, 07:43 PM

Tags for this Thread