How Pointer & Variable Allocate in Heap & Stack Area. If you are working on C++ or C programming then you are already aware of the concept of pointer and variable. These two terms are used to store a reference of a memory location and these two are also used to create objects and arrays.
A variable is an area of memory used for storing data that is used in a program. A variable holds data and can be assigned values at any time by using variables. A pointer is also an area of memory used for holding the address of another memory location. This is a constant value that is assigned in the program. A variable can hold a number of data types, for example, int, float, string, etc.
A pointer is a variable that holds a reference to a location in the memory. So, when you allocate memory for an object then the reference is stored in a pointer variable and when you delete the pointer variable then the pointer is removed from the memory and the space is returned back to the system.
The basic concept of pointers and variables
Now, we know the basic concept of pointers and variables. But, if you are wondering why the pointers and variables need to be allocated in the heap memory and not in the stack area. The main reason behind this is that when we assign the reference to a pointer or a variable then it is assigned to the current stack area of the program. So, if you try to access the pointer or the variable from outside of the current stack then you will get an error.
Stack is a small area of the memory where the temporary values are stored. When the function calls return back to the calling function then the temporary values will be removed from the stack. But, if you want to use the pointer or the variable outside of the calling function then it needs to be stored in the heap.
The stack area is limited, as we cannot assign the pointer or the variable to the stack area. But, if you assign the variable or the pointer to the heap area then you can use the pointer or the variable from anywhere in the program.
Memory Allocation
To allocate the memory we use malloc() and free() function to allocate the memory in heap or stack area. Let us see how pointers and variables allocate memory in heap or stack area.
Pointers and Variables Allocate in Heap Area
Heap is the most important area of memory allocation. We can use this memory for a variable or a pointer.
Memory Allocation Using malloc() Function
malloc() is the function that is used to allocate the memory in the heap. This is a function of C language.
In the following code you can see the main function.
main()
{
int *p = NULL;
p = (int*)malloc(sizeof(int));
}
Here p is a pointer to a integer, we have created a pointer and allocated the memory in the heap. If you see the above code you will notice that the memory is allocated in the stack area. We have not created a pointer, only the integer is allocated. If we allocate memory in the stack then we can only use the memory and it can’t be modified.
Variables and Pointers Allocate in Stack Area
Stack is the second important memory that is allocated. We can create a pointer to a variable and it will be stored in the stack. Let us see how we can allocate the memory in the stack.
Memory Allocation Using calloc() Function
calloc() is also a function of the C language. We can use this function for allocating memory in the stack.
In the following code you can see the main function.
main()
{
int *p = NULL;
p = (int*)calloc(1,sizeof(int));
printf("%d",*p);
}
Here p is a pointer to an integer, we have created a pointer to the integer and allocated the memory in the stack.
What is the output of the following program where address of p is 1200 and q is 1800.
#include
#include
void *fun(int **q)
{
int r = 50;
**q=r;
printf("%u ", *q);
}
int main()
{
int *p = (int *)malloc(sizeof(int));
*p = 55; fun(&p);
printf("%d %u\n", *p, p); return 0;
}
Output: 1200 50 1200
The memory allocated to “p” is allocated by malloc() in main() and the memory is available to other functions as well as in this case. In fun() the function is altering the memory shared by the pointer “p”, which is updated to 50.
Conclusion:
I hope you liked this post about “Pointers and Variables Allocate In Heap or Stack Area”. These are the basic reasons behind why the pointers and variables need to be allocated in the heap. If you don’t understand the above concepts then read the article carefully. I will provide some useful links that will help you to learn the basics.