Memory freed at the end of function?

This is a discussion on Memory freed at the end of function? within the C Programming forums, part of the General Programming Boards category; Hi, I was just wondering about this function I wrote: Code: /*Reverses a string*/ char *strrev(char *in_string) { char *out_string; ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    9

    Memory freed at the end of function?

    Hi,

    I was just wondering about this function I wrote:

    Code:
    /*Reverses a string*/
    char *strrev(char *in_string) {
        char *out_string;
        int in_char,
            out_char;
    
        out_string = (char *) calloc(strlen(in_string), sizeof(char));
        
        in_char = strlen(in_string) - 1;
        
        for (out_char = 0; out_char < in_char+1; out_char ++) {
            out_string[out_char] = in_string[in_char - out_char];
        }
        
        return out_string;
    }
    I never free the memory pointed to by out_string, but I can't, because I have to return that. Am I screwing up somewhere, or is the memory freed when the function returns?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    The memory is not freed when the function returns. Rather, the caller is responsible for freeing the memory, or passing this responsibility somewhere else.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    That's one point of allocating memory dynamically - so that the object could outlive the function where it was created.

    It might be also be possible to reverse the string in-place. If the caller doesn't care about keeping the original string, no extra memory allocation is necessary. But if he does, he'd have to allocate and make a copy of the string himself.

    Yet another option would be also to accept the buffer where to write the output (except one should be aware that the algorithm would mess up the result, if the ranges overlap).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    9
    Thanks guys. I just came to the conclusion I should be freeing the memory in the calling function, but I thought I'd check.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Methinks too! anon has a good point about optimal memory utilization. Why use a buffer as long as the string when it can be reversed in situ by using just one temporary buffer; albeit it'll make your program computationally intensive ie resource tradeoffs.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,261
    Have the caller provide a second buffer to hold the reversed string. It's then the caller's responsibility to provide a buffer at least as big as the other one. The allocation and deallocation then occurs in the same function (if it's even done dynamically at all).
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. Replies: 18
    Last Post: 12-31-2005, 12:56 PM
  5. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM

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