Skip to main content

Command Palette

Search for a command to run...

Understanding Memory Layout: A Comprehensive Guide

Updated
3 min read
Understanding Memory Layout: A Comprehensive Guide
Z

I'm GRIR Zouhair, a dedicated and experienced Full-Stack Developer from Morocco. With a strong passion for coding and a knack for solving complex problems, I excel in creating robust and scalable web applications. My expertise spans across both front-end and back-end technologies, allowing me to deliver comprehensive and seamless solutions.

at the first we need to know what the section of memory

  1. The code segment (also known as the text segment) is a section of a program's memory that contains the executable instructions. The code segment is typically read-only to prevent accidental modification of the program's instructions. Additionally, it is sharable, meaning that while each process thinks it has its own copy of the code segment, they are actually sharing the same physical memory region, reducing memory usage.

     // function code
     void hello() {
         printf("Hello, World!\n");
     }
     // main program logic
     int main() {
         return 0;
     }
     //Constant string literals 
     printf("hello, this is a string literals!");
    

    you can say the binary code (machine instructions) from a.out is loaded into the text segment when the program is executed. and text segment loses its data when you turn off your PC because it resides in volatile memory (RAM)

  2. the data segment (also known as the initialized data segment) is where static and global variables initialized by the programmer are stored. These variables are assigned specific values in the source code and are loaded into memory with those values when the program starts. On the other hand, the BSS segment (Block Started by Symbol) holds global and static variables that are not explicitly initialized by the programmer. The operating system automatically initializes these variables to zero when the program is loaded into memory. While both segments are part of the data segment.

     // global variable
     int global_var = 42;
     // static variable
     void example() {
         static int static_var = 10;
     }
    

    If they are not initialized, they are placed in the uninitialized data segment (BSS) instead.

  3. stack sigment, now i guess you have confusing but why let’s clarify that Local variables (stack memory) and global/static variables (data segment) often seem similar in small programs. But the key difference is scope and lifetime.

    • Stores local variables and function call information (like return addresses).

    • Temporary: Each function call creates its own stack frame, which is destroyed when the function exits.

    •       void example() {
                int local_var = 10;  // Stored in the stack
            }
      

      sooo, local variable inside a function is allocated on the stack.

    • Even if a function doesn't have any local variables, it still creates a stack frame when called. This frame contains the return address

  4. The heap is a region of memory used for dynamic memory allocation, where memory is allocated and freed manually during program execution. This type of memory is not automatically managed, unike stack memory, which is managed automatically by the system.

    You might ask: What could happen if you don’t allocate memory on the heap?

    When you allocate memory for a variable on the stack (local variables within a function), that memory is automatically freed when the function exits. If you return a pointer to this stack-allocated memory, the pointer will point to invalid memory once the function exits, causing undefined behavior.

     #include <stdio.h>
     #include <string.h>
    
     char *ft_localCpy(const char *str) {
         char local_copy[strlen(str) + 1];  // Local array (stack memory)
         strcpy(local_copy, str);  // Copy string to local_copy
         return local_copy;  // Returns pointer to stack memory
     }
    
     int main() {
         char *result = ft_localCpy("Hello");
         printf("%s\n", result);  // Undefined behavior: result points to invalid memory
         return 0;
     }
    

    that’s why ft_strdup is usefull used to copy a string and allocate memory on the heap, ensuring the string remains available for further manipulation after the function exits. Without heap allocation, the string would be lost once the function ends, making it impossible to access or manipulate.

Z
zgd1y ago

I am passionate about game development and am looking for a multiplayer online mobile game source code for personal learning and communication to deepen my understanding of game architecture and backend technology. I highly respect intellectual property rights and am willing to pay reasonable compensation for high-quality source code to express my sincerity and gratitude. What I am pursuing is not just a piece of code, but an opportunity for me to deeply learn and understand the essence of game development. If you have this resource and would like to share it, I would greatly appreciate it. Please contact me through the following methods vlin791864008@gmail.com I will communicate with you at your convenience.

Memory Layout Explained: A Complete Guide