Ocean of Programming

Breaking

Friday, 14 August 2020

Memory Layout of C Programs

August 14, 2020 0
Memory Layout of C Programs

 

        In this article, I am going to explain the Memory Layout of C program before that if you have not read my previous article Compilation Stages in C programing you read that click here

 

     Memory layout of C program consists of 5 segments 

1. Text Segment

2. Initialized Data Segment

3. Uninitialized Data Segment

4. Heap

5. Stack 

 

 Let us see one by one.


1. Text


      The text segment is also known as a code segment which contains the binary of the program(Object Files). It is the lowermost layer of memory layout and a sharable so that only a single copy needs to be in memory for frequently executed programs. The text segment is a read-only segment that prevents a program from being accidentally modified. 


2. Initialized Data Segment

 

     The initialized data segment also called a Data Segment. It contains initialized global and static variables that are initialized by the programmer. It has read and write permission so that the value of the variable can be changed at a run time. 


    This segment can be further classified into an initialized read-only area and an initialized read-write area.

 

3. Uninitialized Data Segment

      

     Uninitialized Data segment is also called as BSS. This segment starts at the end of the data segment. It contains all global variables and static variables that are initialized to zero or do not have explicit initialization and pointer are pointers with the null pointer. 

    Data in this segment are initialized by the kernel to zero before program starts executing. 
  
 

4. Heap

    Heap is the segment is used to allocate memory at the run time.
The heap area begins at the end of the BSS segment and grows to larger addresses from BSS Segment.  This area is managed by malloc, realloc, and free.

    The Heap area is shared by all shared libraries and dynamically loaded modules in a process. When memory needs to be allocated using malloc and calloc function, heap grows upward as shown in the above diagram. 
 

5. Stack

    The stack area adjoined the heap area and grew the opposite direction. The stack contains temporary data such as a local address, function parameters and return address.
    The stack contains a LIFO structure. Function variables are pushed onto the stack when called and functions variables are popped off the stack when return. Stack pointer (SP) register tracks the top of the stack.  When the stack pointer meets the heap pointer, free memory will be exhausted. This segment size is variable as per function calls, local variables, and function parameters.


 
 

Saturday, 8 August 2020

Compilation Stages In C Programming

August 08, 2020 0
Compilation Stages In C Programming

 

         In this article, I am going to explain the compilation stages in C before that we will see some other basics.


What is mean by compilation?

    The compilation is a process of converting the source code into an executable file. It can be done with the help of the compiler such as Code Block if you want to know how to download install code block click here check my previous article. The compiler will check for error and if the code is error-free then it will generate the executable file. The compiler converts the code into machine language or code that a computer's processor uses. The executable file type depends on the compiler. Some of the executable file types are ".exe", ".hex", ".elf", etc.


Before generating the executable the source code passes into 4 phases. 

      - Pre-processing

      - Compiler

      - Assembler  

      - Linker

 

 Now lets us understand each phase in detail. 

 

1. Pre-processing

    This is the first stage of the compilation. In this stage, the line starting with '#' is considered as a pre-processor command. The stage includes  

 

      - Macro definition and expansion

      - Conditional compilation 

      - Removal of Comments 

      - Expansion of the included files

 

    We can execute the command: gcc -E filename.c. It will generate file a filename.i  and results are printed on the standard console. 

 

 

2. Compilation

    In this stage, the expanded code is passed to the compiler and the compiler converts the code into assembly code. 


    We can execute the command: gcc -S filename.c. It will generate file a filename.s and it is human-readable. 

 


3. Assembly

    In this stage, the code is translated into machine code with the help of an assembler. It will create object files.

    We can execute the command: gcc -c filename.c. It will generate file a filename.o and the file is not human readable.


   

4. Linker
    This is the final stage of compilation. The main working of the linker is to combine program object files with library object files.
It will take object files that are generated by the assembler and add pre-compiled library files. The output of the linker is executable.

We can execute the command: gcc -o executablefile filename.c.

If you like this article then please don't forget to share with your friends and subscribe to my blog.