Wednesday 29 June 2016

Stack Memory in Microcontrollers

Stack is a portion of memory that works on the principle of LIFO (last in first out).  It means that last in data will come out first.  In stack two operations are performed. First is PUSH and another is POP. PUSH function is used to save the data in stack memory and POP operation is performed to retrieve the data. In stack memory there is a pointer variable which monitor the top of the stack location in memory.  When a byte is pushed on to the stack this pointer value is increases by one to the next available address. When a byte is popped from the stack, this pointer contained value is decremented by one.

Uses of Stack
When a function is called stack memory is used to temporary store the local variables and return address from the function. On function call, current program counter (PC) value (stores the next instruction to be executed) and status register values are pushed on to the stack.
All the variables declared in to the function are managed in to the stack. This is the advantage of stack that we don required to allocate memory for the variable initialized in to the function. All the variables initialized in the functions are local to that function.
When program exists from the function all the variable popped from the stack and stack memory is cleared. We don’t require clearing this memory. When exist from the function stored status register and PC values are also popped to start execution after the function call.
In stack memory there is a limit of stack memory dependent on operating system. So we can store limited variables on to stack.
Heap Memory
Heap is the type of memory which is not managed automatically as stack memory is managed by the CPU. It is a free memory and there is no limit like stack memory. To allocate and deallocate memory in heap malloc() and calloc() and free() functions are used. Once the memory is allocated for the variables in heap it does not clear after exit form functions until we use free() function to clear the memory. When we use heap memory instead of Stack memory to store variables, these variables can be accessed globally.
Accessing the heap memory is slower as compare to access stack memory because we access the heap memory pointers.
Memory management is required in heap memory.  We have to free the memory location if it not used. If you are not able to free the memory using free() function memory leak problem may occurred.
Heap memory support dynamic memory allocation. It means we can resize the variables using realloc() functions.
malloc() Function
 malloc() function allocate specified size memory location for an object and returns a pointer to it. Syntax of malloc() is as follows:
            void *malloc(size_t size)
 Example:
                   int *ptr1     =       malloc(15);
This will allocate 15 memory location and return a pointer to ptr1.


calloc() Function
 calloc() function is also used to allocate the specified memory location for an object and returns a pointer to it. Syntax of calloc() is as follows:
void *calloc(size_t n, size_t siz)
 in put argument in calloc() is two variables one n, that defines the no of location to be reserved and siz will define the size of each location. The main difference between malloc() an calloc() is that it will initialized all location to Zero.
Example:
            int *ptr2     =       calloc(10,size(int));
realloc() Function
realloc() function used to resize the allocated area for an object and return a pointer according to new relocated memory location. Syntax of calloc() is as follows:
void * realloc (void * ptr, size_t size);
First argument define the address of firstly allocated memory and second argument is the modified size.
Example:
int *p1 =  calloc (5, sizeof (int));
int * p2 = (inrealloc (p1, sizeof(int));
What is Stack over Flow?

Stack has limited memory location. It can store a limited amount of memory location. Stack overflow occurs when try to push larger data from stack memory limitation. When we try to push more data on stack data will start over flowing in the memory and allocating in the other portion of memory location. This will cause the crash of system. To prevent from stack over flow don’t use many nested functions. Not initialize much of variable locally in any function, if it is required to initialize lot of variable then use heap to initialize the variable.

No comments:

Post a Comment