Reputation: 3978
When the memory is allocated to a function. For example:
int doubleMe(int smthng){
int dbl = 2*smthng; //line 2
return dbl;
}
void main(){
int var;
printf("The double of var is: %d",doubleMe(var)); //line 8
}
When is memory allocated to variable dbl
?
I believe it is allocated when function is called(run-time) in stack. And freed when function exits, is it? Would be great if someone could please explain it better.
This question looks similar but not quite!
Upvotes: 11
Views: 11901
Reputation: 23218
Regarding your statement: Memory is allocated to variable dbl(or var) at compile time
No, The instructions to allocate memory are created at compile time.
Memory is not allocated until that function is executed.
Memory created inside a function is an example of local scope memory. if it is created on the stack, it will be released upon leaving the scope in which it was created. If it was created on the heap (i.e. created using [m/c/re]alloc()
functions) it will not be released (or more accurately, made available as described here) until free()
is called, or at program exit.
Later, you state:
I believe it is allocated when function is called. And freed when function exits.
This statement is true, but this all happens at run-time only. Not at compile-time.
For global scope memory, stack memory is created when the program is executed, and is not released until the program ends. Again, for heap memory , it will be released upon calling free()
, or at program exit.
Upvotes: 3
Reputation: 310980
The compiler generates object code of a function when it is defined. The generated code contains instructions to allocate memory in the stack for function local variables or it can use registers to accomodate them.
Where a function is called the compiler generates object code of the function call and corresponding instructions to push arguments on the stack. At this point the compiler may not to know how the function is defined and whether it is defined because its definition can be in some other module or library.
Take into account that the compiler may inline functions even if you yourself do not use function specifier inline
. In this case it will place the function definition in the point where the function is called.
Upvotes: 9
Reputation: 66371
It's not allocated during compilation at all, but at runtime.
When the function is called during execution, either space will be reserved for it in memory (in approximately 100% of current C++ implementations, "on the stack") or it will reside in a register.
If it's not in a register, the space is freed when the function returns.
The compiler produces code that will perform the runtime allocation, if there is any.
Upvotes: 0
Reputation: 213842
Memory is allocated to variable dbl(or var) at compile time in the stack memory
Not correct, it is allocated in run-time, just as all other stack variables. That's the whole point of using a stack. It is allocated when your function is called, in run-time.
I believe it is allocated when function is called. And freed when function exits, is it?
Yes.
Upvotes: 4