Thread: free(something) after RETURN

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    55

    Question free(something) after RETURN

    Let's say I have the following:

    Code:
    char *sendBack(char *someText)
    {
         char *newText = malloc( sizeof(*newText));
         ...
         ...
         ...
         return(newText);
         free(newText);     // will never get reached here
         newText = NULL;     // will never get reached here
    }

    As you can see, I need to "return" something and then free it once I'm done with it. My question is: Since I can't free it here in the sendBack function, can I free it in the calling function? For example, is this legal?:

    Code:
    void printMe()
    {
         char buf[100];
         buf = sendBack("blah blah blah");
         printf("%s\r\n", buf);
    
         free(buf);     // is this freeing *newText?
         buf = 0;
    }
    Something just doesn't look right.

    Any suggestions?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, freeing in the calling function is exactly what you SHOULD do in this case.

    Obviusly, putting code after an unconditional return is only ever going to possibly give you a warning for "unreachable code" if the compiler supports that - but it will never be RUN, so you can delete those two lines straight away.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    55
    Quote Originally Posted by matsp View Post
    Yes, freeing in the calling function is exactly what you SHOULD do in this case.

    Obviusly, putting code after an unconditional return is only ever going to possibly give you a warning for "unreachable code" if the compiler supports that - but it will never be RUN, so you can delete those two lines straight away.

    --
    Mats
    Excellent, thank you. I wasn't sure if freeing buf in the calling function would free what I needed freed.

    Thank you for clarifying.

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Code:
    char *sendBack(char *someText)
    {
         char *newText = malloc( sizeof(*newText));
    I guess you meant:
    Code:
    char *newText = malloc (strlen (*someText));
    Or else you'll get the sizeof the thing being pointed to by newText and that's just a char.

    As well as being able to free the memory in the calling function, you could also allocate the memory in the calling function and then pass the pointer. This may not always be suitable, but as a rule of thumb the function/object that allocates memory should free it too.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by QuantumPete View Post
    I guess you meant:
    Code:
    char *newText = malloc (strlen (*someText));
    With a "+ 1" probably?

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    55
    Quote Originally Posted by QuantumPete View Post
    Code:
    char *sendBack(char *someText)
    {
         char *newText = malloc( sizeof(*newText));
    I guess you meant:
    Code:
    char *newText = malloc (strlen (*someText));
    Or else you'll get the sizeof the thing being pointed to by newText and that's just a char.

    As well as being able to free the memory in the calling function, you could also allocate the memory in the calling function and then pass the pointer. This may not always be suitable, but as a rule of thumb the function/object that allocates memory should free it too.

    QuantumPete
    Again, thanks for the insight. And thank you for the correction on the malloc, you're correct.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Or perhaps:
    Code:
    char *newText = malloc (strlen (someText)+1);
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by QuantumPete View Post
    Code:
    char *sendBack(char *someText)
    {
         char *newText = malloc( sizeof(*newText));
    I guess you meant:
    Code:
    char *newText = malloc (strlen (*someText));
    Or else you'll get the sizeof the thing being pointed to by newText and that's just a char.

    As well as being able to free the memory in the calling function, you could also allocate the memory in the calling function and then pass the pointer. This may not always be suitable, but as a rule of thumb the function/object that allocates memory should free it too.

    QuantumPete
    Since we're all correcting each other - he'll most likely get 4 bytes, eg the size of a pointer.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by IceDane View Post
    Since we're all correcting each other - he'll most likely get 4 bytes, eg the size of a pointer.
    My vote is for 1 byte:
    Code:
    char *newText = malloc( sizeof(*newText));
    sizeof(*newText) == sizeof(char) == 1.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Do you really need to dynamically allocate the newtext?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  2. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  3. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM
  4. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM