Thread: Memory freed at the end of function?

  1. #1
    Registered User
    Join Date
    Nov 2009

    Memory freed at the end of function?


    I was just wondering about this function I wrote:

    /*Reverses a string*/
    char *strrev(char *in_string) {
        char *out_string;
        int in_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
    The memory is not freed when the function returns. Rather, the caller is responsible for freeing the memory, or passing this responsibility somewhere else.
    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

  3. #3
    The larch
    Join Date
    May 2006
    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
    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
    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
    New Zealand
    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, 01: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