C programs in memory

To illustrate how a running C program is organized in memory, let's consider a sample C program and then depict its memory layout using an ASCII diagram. The memory of a running C program is typically divided into several segments:

  1. Code Section: Contains the compiled code (machine code) of the program.

  2. Data Section: Stores global and static variables that are initialized.

  3. BSS Section: Holds uninitialized global and static variables. The operating system sets all memory locations in this to zero before starting the program.

  4. Heap: Used for dynamic memory allocation.

  5. Stack: Stores local variables, function parameters, return addresses, etc.

Here's a sample C program that includes various types of variables and memory allocations:

#include <stdlib.h>

int globalVar = 5;          // Global variable
int globalVarUnInit         // Global variable - uninitialized
static int staticVar = 10;  // Static variable

void exampleFunction(int param) {
    static int staticLocal = 3;  // Static local variable
    int localVar = 4;            // Local variable
    char *dynamicVar = malloc(10 * sizeof(char));  // Dynamic memory allocation

    // ... rest of the function
}

int main() {
    const char *strLiteral = "Hello, World!";  // String literal
    exampleFunction(2);
    return 0;
}

Now, let's create an ASCII diagram to represent how this program would be laid out in memory:

+---------------------------------------+  // lower memory addresses
|    Code Section                       |
| - main                                |
| - exampleFunction                     |
+---------------------------------------+
|  Read-Only Data                       |
| - "Hello, World!" (String Literal)    |
+---------------------------------------+
|     Data Section                      |
| - globalVar                           |
| - staticVar                           |
+---------------------------------------+
|       BSS                             |
| - globalVarUnInit                     |
| - staticLocal                         |        
+---------------------------------------+
|       Heap                            |
| - memory that the address             |
|   in dynamicVar points to             | 
|   |                                   |  // heap grows towards higher memory
|   |                                   |  // addresses
|   \/                                  |
|                                       |
+---------------------------------------+
|       Stack                           |
|   /\                                  |
|   |                                   |  // stack grows towards lower memory
|   |                                   |  // addresses
| - param (exampleFunction)             |
| - dynamicVar (exampleFunction)        |
| - localVar (exampleFunction)          |
| - return address                      |
| - strLiteral (main)                   |
+---------------------------------------+  // higher memory addresses

Explanation:

  • Code Section: Contains the machine code for main and exampleFunction.

  • Read-Only Data Section: Includes the string literal "Hello, World!". This section is read-only to protect the literals from accidental modification.

  • Data Section: Stores globalVar and staticVar since they are initialized.

  • BSS Section: Holds and staticLocal and globalVar since they're uninitialized at the start.

  • Heap: Memory allocated using malloc.

  • Stack: For exampleFunction, the stack includes param, dynamicVar, localVar, and the return address to main. It also includes the strLiteral pointer in the main function.

0
Subscribe to my newsletter

Read articles from Jyotiprakash Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jyotiprakash Mishra
Jyotiprakash Mishra

I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class. At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out. In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.