![]() ![]() Heap is the most important area of memory allocation. Pointers and Variables Allocate in Heap Area Let us see how pointers and variables allocate memory in heap or stack area. To allocate the memory we use malloc() and free() function to allocate the memory in heap or 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. The stack area is limited, as we cannot assign the pointer or the variable to the stack area. 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. When the function calls return back to the calling function then the temporary values will be removed from the stack. Stack is a small area of the memory where the temporary values are stored. So, if you try to access the pointer or the variable from outside of the current stack then you will get an error. 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. But, if you are wondering why the pointers and variables need to be allocated in the heap memory and not in the stack area. Now, we know the basic concept of pointers and variables. How Pointer & Variable Allocate in Heap & Stack Area The basic concept of pointers and variables 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. A variable can hold a number of data types, for example, int, float, string, etc.Ī pointer is a variable that holds a reference to a location in the memory. This is a constant value that is assigned in the program. A pointer is also an area of memory used for holding the address of another memory location. A variable holds data and can be assigned values at any time by using variables. These two terms are used to store a reference of a memory location and these two are also used to create objects and arrays.Ī variable is an area of memory used for storing data that is used in a program. If you are working on C++ or C programming then you are already aware of the concept of pointer and variable. How Pointer & Variable Allocate in Heap & Stack Area. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |