
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Memory Layout of C Programs
The memory layout for C programs is like below. There are few levels. These are −
- Stack Segment
- Heap Segment
- Text Segment
- Data segment
Now let us see what are the functionalities of these sections.
Sr.No | Sections & Description |
---|---|
1 | Stack The process Stack contains the temporary data such as method/function parameters, return address and local variables. It is an area of memory allotted for automatic variables and function parameters. It also stores a return address while executing function calls. Stack uses LIFO (Last- In-First-Out) mechanism for storing local or automatic variables, function parameters and storing next address or return address. The return address refers to the address to return after completion of function execution. This segment size is variable as per local variables, function parameters, and function calls. This segment grows from a higher address to a lower address. |
2 | Heap This is dynamically allocated memory to a process during its run time. This is area of memory allotted for dynamic memory storage such as for malloc() and calloc() calls. This segment size is also variable as per user allocation. This segment grows from a lower address to a higher address. Let us now check how the segments (data and bss segments) size vary with a few sample programs. Segment size is known by executing the command “size”. |
3 | Text This includes the current activity represented by the value of Program Counter and the contents of the processor's registers. It is represented by .text section. This defines an area in memory that stores the instruction codes. This is also a fixed area. |
4 | Data This section contains the global and static variables. It is represented by .data section and the .bss. The .data section is used to declare the memory region, where data elements are stored for the program. This section cannot be expanded after the data elements are declared, and it remains static throughout the program. The .bss section is also a static memory section that contains buffers for data to be declared later in the program. This buffer memory is zero-filled. |
The data segments can be divided into more two parts.
Sr.No | Sections & Description |
---|---|
1 | Initialized data segment This is a portion of the object file or program’s virtual address space that consists of uninitialized static and global variables. Un-initialized data segment is also called BSS (Block Started by Symbol) segment. |
2 | Un-initialized data segment This is read-write, since the values of variables could be changed during run time. This segment also has a fixed size. |
- Related Articles
- Get the information about the memory layout of the masked array in Numpy
- C/C++ Tricky Programs
- Application Programs vs System Programs
- What are destructors in C# programs?
- What are constructors in C# programs?
- Different Star Pattern Programs in C#
- Overview of MySQL Programs
- Why is Java slower than C++ programs?
- What is default constructor in C# programs?
- C++ Programs To Create Pyramid and Pattern
- Memory representation of Binomial Heap in C++
- Div Layout vs. Table Layout in HTML
- What is a parameterized constructor in C# programs?
- C programs to differentiate array of structures and arrays within a structure
- Recursive Programs to find Minimum and Maximum elements of array in C++

Advertisements