Memory layout of C program in linux



Share



Learn memory layout of C program such as text segment, heap segment, stack segment, command line arguments in linux c with examples.

Online courses related to this article


Course description

Learn, test your C programming skills and earn certificate for software jobs



Course description

Learn to develop embedded systems, interfacing electronic peripherals through real time projects and get required practical skills for software jobs



Course description

Learn to develop software solutions for linux environment, implement requirements through real time projects and get required practical skills for software jobs



Course description

Learn firmware development theory, applications, get practical skills from online tests, projects and earn certificate for software jobs

Memory layout of a C program

    Let us understand the memory layout of running c program.
Text Segment Text Segment is also named as text and it contains executable instructions. This is the lower address occupied by the program in memory. Usually, the text segment is sharable so that only a single copy needs to be in memory for frequently executed programs, such as text editors, the C compiler, the shells, and so on. Also, the text segment is often read-only, to prevent a program from accidentally modifying its instructions.
Data Segment Initialized Data Segment is also named as data. A data segment is a portion of virtual address space of a program, which contains the global variables and static variables that are initialized by the programmer. static int i = 10 will be stored in data segment and global int i = 10 will also be stored in data segment.
BSS segment Uninitialized Data Segment is also named as bss. uninitialized data starts at the end of the data segment and contains all global variables and static variables that do not have explicit initialization in source code.
For instance a variable declared static int i; would be contained in the BSS segment.
Heap Heap is the segment where dynamic memory allocation usually takes place.
The heap area begins at the end of the BSS segment and grows to larger addresses from there. The Heap area is managed by malloc, realloc, and free, which may use the brk and sbrk system calls to adjust its size. The Heap area is shared by all shared libraries and dynamically loaded modules in a process.
StackStack is where automatic and temporary variables are stored, along with information that is saved each time a function is called. Each time a function is called, the address of where to return to and certain information about the caller’s environment, such as some of the machine registers, are saved on the stack. The newly called function then allocates room on the stack for its automatic and temporary variables. This is how recursive functions in C can work. Each time a recursive function calls itself, a new stack frame is used, so one set of variables doesn’t interfere with the variables from another instance of the function.
A stack pointer register tracks the top of the stack; it is adjusted each time a value is pushed onto the stack. The set of values pushed for one function call is termed a stack frame; A stack frame consists at minimum of a return address.
The difference between program counter and stack pointer is that the program counter is a register which holds the address of next instruction when the current instruction is under execution while stack pointer is a pointer which points to the top element of the stack.
Command Line arguments This is the highest address occupied by the program in memory.

C program to demonstrate memory layout

Let's demonstrate the memory layout of a C program using few example programs. To begin with, let's write a simple c program to add two integers.


#include "stdio.h"

int result1;

int main()
{
	int p1 = 10,p2 = 20;
	result1 = add(p1,p2);
	printf("The result is %d\n",result1);
	return 0;
}

int add(int a,int b)
{
	return a+b;
}
Program output:
===============
# ./a.out
The result is 30
//size command output
# size ./a.out
   text    data     bss     dec     hex filename
    830     292       8    1130     46a ./a.out

To Read Full Article

Subscribe courses or Pay Per Course & logged in

Exercises, Solutions

Medium
High
Medium
Medium
Medium
Low
Low
Medium
High
High

Want to contribute a new article? Write and upload your article information .
Share


 Articles

 C Programming

 Linux Software

 Embedded Systems

 Python Programming

 Search Code Snippets

 Popular C search examples

 Popular C search MCQs



 Online certificate courses

 Test your skills:online tests

 C Programming

 Python Programming

 Linux Software

 Quantitative Aptitude

 Embedded System Software