help understanding free and memory leaks

This is a discussion on help understanding free and memory leaks within the C Programming forums, part of the General Programming Boards category; Hello, If I understood correctly (and it wouldn't be the first time if I did not), when allocating a local ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    31

    help understanding free and memory leaks

    Hello,

    If I understood correctly (and it wouldn't be the first time if I did not), when allocating a local array or struct inside a function one should free it before finishing the function, in order to avoid memory leaks. Am I correct? This may become a huge issue with programs with a GUI, which stay open for a long time and repeatedly call that function.

    How about local variables defined within a function? I am guessing it is not a big deal to free them, because they do not get allocated a contiguous block of memory. However, is it considered a "good programming habit" to free local variables? Does it even make sense to think of freeing a local variable?

    Thank you very much for you help,

    mc61

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    975
    The easiest thing to remember is this: For each malloc() you should have a free(). That's not bulletproof but it's generally the right thing to do.

    When you're asking about freeing local variables, I'm not sure what precisely you mean. If you asked for memory via malloc(), then yes, free it when you're done. Perhaps in the same function, or if you need to use the memory longer, whenever is most convenient. This is correct (assume malloc() succeeds):
    Code:
    void f(void)
    {
      char *x;
      x = malloc(some_length);
      snprintf(x, some_length, "foo%s", some_string);
      free(x);
    }
    But you wouldn't do this:
    Code:
    void f(void)
    {
      char x[50];
      free(x);
    }
    This is bad because you never allocated x yourself. When the function exits, x will be destroyed automatically. That's what happens with local (or more precisely, automatic) variables.

    A memory leak is when you forget to free something; often as a result of "losing" the pointer:
    Code:
    void f(void)
    {
      char *x;
      x = malloc(50);
      x = NULL;
      /* oops.. can't free the 50 bytes now */
    }

  3. #3
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    I didn't know you could declare structs inside a function ??

    Eitherways, I don't think you need to free up arrays, not even sure if you can do that. What you need to do is free up nodes in linked lists and such. But I could be wrong. What do you mean by freeing up structs though? You can't really free them, and even if you could, why would you, it's essential for the running of the program that utilizes the variables defined within it.

    You don't need to free local variables in functions, because I think once the function ends, they are lost.
    =========================================
    Everytime you segfault, you murder some part of the world

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    It's possible to declare or define a struct inside a function.
    Everything that is allocated by malloc must be freed by free. That simple. This includes arrays and everything else allocated via malloc. Do not call free of anything else. That's undefined behavior.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    31
    Thanks guys. As it has happened many times in this board, you clarified a few misconceptions I had.

    mc61

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  3. Pointer's
    By xlordt in forum C Programming
    Replies: 13
    Last Post: 10-14-2003, 02:15 PM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  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