Memory Layout
Understanding Memory Organization in C Programs
Introduction to Memory Layout (मेमोरी लेआउट का परिचय)
C program का memory layout different segments में organize होता है। हर segment का अपना specific purpose होता है।
A C program's memory layout is organized into different segments. Each segment has its specific purpose.
Key Points (मुख्य बिंदु):
- Memory layout program execution को manage करता है
- Memory layout manages program execution
- Different segments different types के data को store करते हैं
- Different segments store different types of data
- Memory layout program security और performance को affect करता है
- Memory layout affects program security and performance
Text Segment (Code)
Data Segment (Initialized Data)
BSS Segment (Uninitialized Data)
Heap (Dynamic Memory)
Stack (Function Calls)
Memory Segments (मेमोरी सेगमेंट्स)
Memory segments और उनका purpose:
Memory segments and their purposes:
Segment (सेगमेंट) | Description (विवरण) | Example (उदाहरण) |
---|---|---|
Text Segment | Program instructions store होते हैं | Function definitions |
Text Segment | Stores program instructions | Function definitions |
Data Segment | Initialized global variables | int x = 10; |
Data Segment | Initialized global variables | int x = 10; |
BSS Segment | Uninitialized global variables | int y; |
BSS Segment | Uninitialized global variables | int y; |
Heap | Dynamic memory allocation | malloc(), calloc() |
Heap | Dynamic memory allocation | malloc(), calloc() |
Stack | Function calls और local variables | Function parameters |
Stack | Function calls and local variables | Function parameters |
Stack Frame (स्टैक फ्रेम)
Function call के दौरान stack frame का organization:
Stack frame organization during function calls:
void function(int a, int b) {
int local1 = 10;
int local2 = 20;
// Function body
}
int main() {
function(5, 10);
return 0;
}
Return Address
Previous Frame Pointer
Parameter b (10)
Parameter a (5)
Local Variable local2 (20)
Local Variable local1 (10)
Heap Management (हीप मैनेजमेंट)
Heap memory का management:
Heap memory management:
// Dynamic memory allocation
int *arr = (int*)malloc(5 * sizeof(int));
if (arr != NULL) {
// Use allocated memory
for (int i = 0; i < 5; i++) {
arr[i] = i * 10;
}
// Free memory
free(arr);
}
Heap Management Points (हीप मैनेजमेंट पॉइंट्स):
- Memory allocation और deallocation
- Memory allocation and deallocation
- Memory leaks से बचना
- Avoiding memory leaks
- Proper error handling
- Proper error handling
Best Practices (सर्वोत्तम प्रथाएं)
Memory management के best practices:
Best practices for memory management: