Reputation: 2115
When we 'define' a variable inside a function (not main here), is the memory allocation done at runtime or the loader serves for us??
And what happens when i have :
int f()
{
int a=10;
........
}
main()
{
int i;
scanf("%d",&i);
while(--i)
f();
..........
}
Is 'a' in function f() created 'i' times?? And so is it dynamic allocation??
Upvotes: 2
Views: 588
Reputation: 190
Generally, when you define a variable within a function, the memory allocation is done at runtime and the variable is stored within the stack, which represents the storage system for these kind of variables: local variables.
Yes. In the example you provide, the variable a is created 'i' times following this process:
While Loop
Function f()
Push variable a into stack
Function f logic
Pull variable a from stack
End Function f()
End While
So the general behavior for local variables (Variables within a function or block) is to be allocated upon declaration within their scope (function or block) by being pushed inside the stack and to be "freed" when the scope finalizes (end of function or block) by being pulled from the stack.
This is not dynamic allocation but automatic allocation. You can perform dynamic allocation only by using the new operator and your variables will be allocated at runtime within a special area of the memory we call the heap. You have to be careful with dynamic allocation, because you need to explicitely use the delete operator in order to free it.
Note: The former explanation about the stack was a simplification. The variables are not really pulled from the stack. The stack is controlled with two pointers: one points to the base of the stack, and the other points to the top, the next available position. When a function is called, its variables are added to the stack modifying the position of the second pointer, when the function ends, this pointer takes the same value it had at the beginning of the function effectively freeing the memory used within the function. Therefore the variables are not really pulled, you will just overwrite them in following iterations with the stack.
For more information you can refer to C++ Primer Plus Chapter 9. Memory Models and Namespaces
Upvotes: 0
Reputation: 13025
Each time f()
is called, a
will be created on stack. When you compile the code and the compiler generates binary equivalent of the code, the compiler writes necessary instruction to allocate space on stack for a
. It is nothing but incrementing/decrementing a CPU register (known as stack pointer). Since the increment/decrement of stack pointer is done by the compiler (by writing instruction on binary) it is not dynamic allocation. The space will be recovered automatically after function returns. This is also accomplished by changing the value of stack pointer and compiler writes necessary instruction on binary to do that.
Dynamic memory allocation refers to a process when the compiler doesn't reserve memory for you. Instead, the space is allocated by Operating System at runtime. And the space comes from heap area of RAM. The programmer is responsible to free that space and compiler won't do anything about it. When dynamic memory allocation is coded like with new
:
int *prt = new int[10];
the compiler doesn't reserve memory for 10 integers. Instead, a request is made to the OS to reserve that memory. When this code is executed, OS reserves the memory (if enough memory is available), and returns a pointer to the beginning of the memory.
Upvotes: 0
Reputation: 11648
Q1. Yes.. It will be allocated i
times in the Stack..
Q2. No.. The memory allocated using new
keyword is dynamic memory allocation which will be allocated in heap and the allocation made in the example you provided is stack allocated..
Upvotes: 1
Reputation: 273179
The local variable a
is made during each call of f()
. It is part of setting up the 'stack-frame' for f()
and costs (almost) nothing in time. It eats up a little stackspace, but no more than is necessary for an int
.
During while(--i) f();
the function f()
is called 10 times and each time a 'new' a
occupies the same spot of memory. We do not call this dynamic allocation, it is called stack, local or auto allocation.
Upvotes: 11
Reputation: 6233
Variables are allocated on the stack, and are not "dynamic memory". Yes, a will be put on the stack i times.
Upvotes: 0
Reputation: 5525
This is a stack allocation, meaning that place is reserved on the stack for the integer - it's not allocated like "find 4 free bytes on the heap and allocate them for me".
Upvotes: 1