functions returning a string or other buffer: need to free it?

This is a discussion on functions returning a string or other buffer: need to free it? within the C Programming forums, part of the General Programming Boards category; I'm wondering if I generally have to free a string/buffer returned by a function if the documentation doesn't mention this. ...

  1. #1
    not-a-geek
    Join Date
    Apr 2004
    Posts
    210

    functions returning a string or other buffer: need to free it?

    I'm wondering if I generally have to free a string/buffer returned by a function if the documentation doesn't mention this.

    In case of i.e g_strdup() the documentation mentions that I should free the returned buffer but then there are also functions like inet_ntoa() where the documentation doesn't say anything about this.

    I'm also wondering what happens if I use such a function in the parameterlist of another function without ever storing a pointer in my code. Is that memory lost or is there a mechanism in place to handle this?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    no, you generally won't need to free the data in those cases - it's global data managed by the C-runtime environment. but you SHOULD copy the data immediately and not store pointers to it. there may also be some issues when using those functions in multithreaded applications - read the documentation to be certain.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    char *freemymemory( void )
    {
        char *s = malloc( sizeof( "Hello World!" ) );
        strcpy( s, "Hello World!" );
        return s;
    }
    
    char *donotfreeme( void )
    {
        static char buf[BUFSIZ] = "Hello World!";
        return buf;
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    not-a-geek
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by quzah
    char *donotfreeme( void )
    {
    static char buf[BUFSIZ] = "Hello World!";
    return buf;
    }
    [/code]
    The problem is that I can't look into someone elses' function. Btw, the example would not work without use of static, right?

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Btw, the example would not work without use of static, right?

    right.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by Nyda
    The problem is that I can't look into someone elses' function.
    This is why it should be mentionned in the documentation. If you have no doc, the only hope is some smart comment in the header file where the prototype is defined... If not, all bets are off.
    Btw, the example would not work without use of static, right?
    Correct.
    Emmanuel Delahaye

    "C is a sharp tool"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  2. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 11:47 AM
  4. a menu system with string functions
    By pxleyes in forum C Programming
    Replies: 11
    Last Post: 04-08-2004, 11:08 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21