0

Just wondering if anyone can explain why we are concerned with memory leak in the heap (freeing for example a char *), but we are not concerned with freeing variables that are in the stack in the main function.

2 Answers 2

1
  void functionA();
  void functionB();
  int main()
  {
      int a = 10; // there are now 4 bytes (assuming 32 bit!) of memory in use on the stack
      int b = 10; // there are now 8 bytes of memory in use on the stack
      functionA(); // scroll down to follow
      /**
       *  The bytes allocated on the stack in functionA where automatically released when the 
       *  function completed. However, we allocated 4 bytes on the heap that we didn't free.
       *  Current status is Stack: 8 Heap: 4
       */
       
      functionB(); // scroll down to follow
      /**
       *  Again, the bytes allocated on the stack in functionB were automatically released
       *  Current status is Stack: 8  Heap: 4
    
  
      return 0;
      /**
       *  main() has just completed. The 8 bytes on the stack are automatically released.
       */

  
  void functionA()
  {
      int c = 10; // there are now 12 bytes of memory in use on the stack
      int* d = malloc(sizeof(int)); 
      /* there are now 20 bytes in use on the stack
       *  pointers are 8 bytes, at least on my system
       *  in addition, there are 4 bytes of memory now in use on the **heap**
       */
      
      return; 
      /**
       *  there are now 8 bytes of memory in use on the stack
       *  return isn't necessary to call from a void function, I'm just showing what
       *  happens when the function completes.  Follow back up to main
       */
  }
  
  void functionB()
  {
      int* d = malloc(sizeof(int)); 
      /**
       *  there are now 16 bytes of memory in use on the stack
       *  and 8 bytes on the heap with this new malloc call
       */
      free(d); 
      /*  this released the memory reserved on the heap by d, so we're down to 4 bytes 
       *  on the heap, still 16 on the stack
       */
      return;  
      /**
       *  again, just for illustration. Now we are back down to 8 bytes on the stack
       *  and 4 on the heap. Follow back up to main
       */
  }

But what about those 4 bytes that were left on the heap?
When is stack space allocated? When is it actually returned to the OS?

1
  • Ok. Thanks for the explanation! This is what I thought was happening. This means that using variables unnecessarily in main would have the same negative effect in terms of memory allocation as not freeing a char* because they are never released until the end of the program. Commented Nov 22, 2014 at 8:46
0

The stack goes off when the function exits, destroying all its local variables with it. That's why you can't leak memory in the stack. e.g.,

void func(void)
{
    char* s = malloc(256);
    char t[256];
}

When the function exits, s, t, and the memory t points to will be taken off the stack, returning its ownership to the OS. So, you can't leak memory. However, the memory gained by malloc remains, without any access as t was destroyed. So you have a leak of 256 bytes, not 512 bytes.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .