2
void local () {
    int x = 100;

    double *loc;
    loc = (double *) malloc(sizeof(double)*100);
    memset(rxnrun, 0x0, sizeof(double)*100);

    // perform some operations


    // free(loc);
    return; // without doing free for loc variable
}

here, i can see what is wrong as memory leak is there for loc variable. But, what about variable x? What will happen to memory space acquired by both variable if we leave variable x as well as loc variable unattended (not free) after function return?

Will they (both variables) still acquire space?

sampathsris
  • 21,564
  • 12
  • 71
  • 98
Vishwadeep Singh
  • 1,043
  • 1
  • 13
  • 38
  • 1
    Variable `x` is stored in stack space, hence it is popped out. Simply put the stack pointer is incremented so that space previously occupied by `x` can be overwritten. For `loc` memory is allocated from heap, hence the memory remains allocated till the end of program, after that OS cleans it up. – 0xF1 Sep 24 '14 at 06:29
  • possible duplicate of [What and where are the stack and heap?](http://stackoverflow.com/questions/79923/what-and-where-are-the-stack-and-heap) – Mantosh Kumar Sep 24 '14 at 10:54

3 Answers3

6

Actually, both the x and loc variables are local function variables, and both variables are freed when the function returns.

However, loc is a pointer and the memory the pointer points to is what is not freed. It's just the memory allocated by malloc() that is not freed.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
3

All C implementations use a call stack. So a local variable is on that stack (or in a register) and disappears when the called function returns.

In theory, C does not require any call stack. In practice, all C implementations use one.

In fact, a local variable is lost (so disappears) when the block (between braces) declaring it is exited. Actually, the slot on the call frame may be reused, or the variable sits in a register which is often reused for other means.

When coding in C you should think as if every local variable disappears when its block is exited. The compiler is free to implement that abstraction as it wants (generally, using a call stack, or keeping that variable in some register or some other area which will be reused later).

Notice that in your example both x and loc are local variables. But loc contains a pointer to a memory zone on the heap. And that zone is released by free

Read about undefined behavior, e.g.

// WRONG CODE!
int* p = 0;
{ 
   int y = 23;
   p = &y;
};
int z = *p; // undefined behavior

Here y is a local variable to the block in braces. You store its address in p. When you dereference that address to put its content into z you have an undefined behavior and the implementation can do anything (including destroying the universe, or exploding your computer) and still be conformant to the specification of C.

What happens in reality is unpredictable without knowing the specific implementation and environment. Actually, and this is bad for you, the above code might not even "fault" and something could be stored in z.

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
-1

variables x and loc will be forgotten, but memory allocated by malloc will be occupied but pointer to this memory will be forgotten.

nothing good will come.

Ivan Ivanovich
  • 880
  • 1
  • 6
  • 15