C language

Program Memory – Introduction

This article is very important for the person who wants to understand c language. Anyone who is going for any interview for a good software company must read the following article as the questions asked in the tests and interviews need the candidate to be clear in the following concepts. This article would ensure that the answers to the following questions are answered properly:

How are variables and pointers actually represented at the machine level?

What is the reason behind the dangling pointers?

Why do the dereference error occurs?

Why do we have other bugs on pointers?

Hence I recommend every person who wants to have a good understanding to read this article. I have explained concepts by transforming them into the form of questions. The article has been written in such a way that it becomes very simple for the user to understand this.

In C language we have different types of variables like global variable, local variable, parameter to the function and dynamically allocated variable.

 But do we know how memory is allocated to these variables and where are these variables stored?

This concept is very important to understand. So let’s discuss this and understand the language better which would help us in dealing with many errors in the program.

We consider 3 kinds of memory in a computer system

FIXED MEMORY:

This memory is used to store the program written or the application being run (i.e. the executable code) and the constant variables or other constant structures. The global and the static variables are also stored in the fixed memory.   This memory is categorized into code memory, data memory etc. The code memory stores the executable code while the data memory stores the global and the static variables.

STACK MEMORY:

The stack memory is a data structures which follows the policy of Last In First Out (LIFO).The stack memory is used to store the static data which includes the variables which are declared at the start of the program like the fixed arrays, local variables and the parameters which are passed to a function during the function call. Other than this the return address pointer, frame pointer (the pointer which is used to point to the starting of the frame just below) and other registers. The life of the static variables ends as soon as the function returns or the program execution stops. The stack memory is used separately for separate programs in an operation system. The memory in stack is allocated at the compile time.

Disadvantage: The memory declared first remains the same i.e. if we declare large memory then there is wastage of memory while if we declare lesser memory then we are in a problem.

Advantage: The memory allocated is deallocated automatically after the function returns and hence the same memory is available for re-use again.

HEAP MEMORY

The heap memory is used to allocate space to the dynamic data. The data whose size can shrink and expand as the requirements vary. We can allocate the extra memory whenever required using command such as malloc(), calloc(), realloc() and de-allocate the space already allocated using command such as free(), delete(). The memory in heap is allocated at the run time.

Wecan take HEAP memory as a group of nodes (circles) and every node is reachable only if It has a direct or indirect reference from some root node (pointer) which is stored in STACK memory. When we use memory allocation functions like malloc, calloc or realloc the memory node are selected and linked to the specified root node. Now if we don’t free the nodes of HEAP memory before the root node is de-allocated, the reference to those memory nodes is lost and they become unreachable, hence known as garbage memory or memory leak. Hence to avoid this problem the programmer must free the memory after it’s use.

Advantage: This memory can be anytime shrunk or expanded when ever the requirements of the program changes.

Disadvantage: We have to manually deallocate the memory allocatd using function like free or delete, otherwise the memory which is not deallocated at the proper time goes to the garbage and is not available for re-use.

We can represent the above memories in a diagram as

www.exploreroots.com

Example

Consider the program

int x=4;

main()
{
    int z;
    char* y;
    y= malloc(6);
    y = "Hello";
    printf(“%c”, *y);
    z = square(x);

    printf(“%d”, z);

    free(y);

}

int  square(Int k)
{
    return k*k;
}

 Now we see which variables in the program are stored where.

In this program we have different types of variables like x as global variable, z & k as local variable and as parameter to the function and y is dynamically allocated variable. So x is stored in fixed memory, z & k are stored in Stack Memory and the pointer y is also stored in stack memory while the 6 bytes (allocated by malloc) which are pointees of pointer y are stored in heap memory.

Leave a Reply

Your email address will not be published. Required fields are marked *