Dynamic Allocation
Given a program:
int i;
int main()
{
int j;
int *k = (int *) malloc (sizeof(int));
... }
Where are each of these variables stored?
Solution:
(int *) malloc (sizeof(int)) --- this will allocate memory in heap of size int.
int *k --- this will allocate memory in call stack and point to memory present in the heap
int i;
int main()
{
int j;
int *k = (int *) malloc (sizeof(int));
... }
Where are each of these variables stored?
Solution:
(int *) malloc (sizeof(int)) --- this will allocate memory in heap of size int.
int *k --- this will allocate memory in call stack and point to memory present in the heap
Comments
Post a Comment