at the first we need to know what the section of memory
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)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.
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
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.