Home  >  Article  >  Backend Development  >  Memory layout of C program

Memory layout of C program

WBOY
WBOYforward
2023-08-26 22:29:111212browse

The memory layout of the C program is as follows. There are very few levels. They are-

  • Stack segment
  • Heap segment
  • Text segment
  • Data segment

Memory layout of C program

Now let’s see what are the functions of these parts.

Sr.No Section and Description
1 Stack

The process stack contains temporary data such as method/function parameters, return addresses, and local variables. It is an area of ​​memory allocated to automatic variables and function parameters. It also stores the return address when executing a function call. The stack uses the LIFO (last in first out) mechanism to store local or automatic variables, function parameters, and to store the next address or return address. The return address refers to the address to which the function returns after completion of execution. The size of this segment varies based on local variables, function parameters, and function calls. The segment grows from higher to lower addresses.

2 Heap

This is memory allocated dynamically while the process is running. This is the area of ​​memory allocated for dynamic memory storage (such as malloc() and calloc() calls). The segment size also varies based on user allocation. The segment grows from lower addresses to higher addresses.

Now let us check how the segment (data and bss segment) sizes change for a few example programs. The segment size can be obtained by executing the command "size".

3 Text

This consists of the value of the program counter and the processor registers Content represents the current activity. It is represented by the .text part. This defines the area in memory where the instruction code is stored. This is also a fixed area.

4 Data

This section contains global variables and static variables. It is represented by .data section and .bss. The .data section is used to declare a memory area where data elements are stored for the program. Once a data element is declared, this section cannot be expanded and remains static throughout the program. The

.bss section is also a static memory section that contains buffers for data declared later in the program. This buffer memory is zero-filled.

The data segment can be divided into two more parts.

Sr.No Part and description 1 Initialization data segment

This is part of the target file or program virtual address space, consisting of uninitialized static variables and global variables. The uninitialized data segment is also called the BSS (Block Started by Symbol) segment.

2 #Un - initialization data segment

This is readable and writable because The value of a variable can be changed at runtime. The segment also has a fixed size.

The above is the detailed content of Memory layout of C program. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete