Memory Management

Understanding Memory Allocation and Management in C Programming

Introduction to Memory Management (मेमोरी मैनेजमेंट का परिचय)

Memory management C programming में memory को allocate और deallocate करने का process है। ये program की efficiency और reliability को improve करता है।

Memory management is the process of allocating and deallocating memory in C programming. It improves program efficiency and reliability.

Key Points (मुख्य बिंदु):

  • Memory management memory leaks को prevent करता है
  • Memory management prevents memory leaks
  • Different memory segments different purposes के लिए use होते हैं
  • Different memory segments are used for different purposes
  • Proper memory management program performance को improve करता है
  • Proper memory management improves program performance
Stack Memory
Heap Memory
Static Memory

Memory Segments (मेमोरी सेगमेंट्स)

Different memory segments और उनका use:

Different memory segments and their usage:

// Stack memory example
void stack_example() {
    int local_var = 10;  // Stack memory
    printf("Local variable: %d\n", local_var);
}

// Heap memory example
void heap_example() {
    int *ptr = (int*)malloc(sizeof(int));  // Heap memory
    *ptr = 20;
    printf("Heap variable: %d\n", *ptr);
    free(ptr);
}

// Static memory example
static int global_var = 30;  // Static memory
Memory Segment (मेमोरी सेगमेंट) Description (विवरण)
Stack Local variables और function calls के लिए
Stack For local variables and function calls
Heap Dynamic memory allocation के लिए
Heap For dynamic memory allocation
Static Global और static variables के लिए
Static For global and static variables

Memory Functions (मेमोरी फंक्शन्स)

Memory management functions और उनका use:

Memory management functions and their usage:

// malloc example
int *arr = (int*)malloc(5 * sizeof(int));
if (arr == NULL) {
    printf("Memory allocation failed\n");
    return;
}

// calloc example
int *zeros = (int*)calloc(5, sizeof(int));
if (zeros == NULL) {
    printf("Memory allocation failed\n");
    return;
}

// realloc example
arr = (int*)realloc(arr, 10 * sizeof(int));
if (arr == NULL) {
    printf("Memory reallocation failed\n");
    return;
}

// free example
free(arr);
free(zeros);
arr = NULL;  // Prevent dangling pointer
zeros = NULL;

Memory Functions (मेमोरी फंक्शन्स):

  • malloc(): Memory allocate करने के लिए
  • malloc(): To allocate memory
  • calloc(): Zero-initialized memory allocate करने के लिए
  • calloc(): To allocate zero-initialized memory
  • realloc(): Memory size को change करने के लिए
  • realloc(): To change memory size
  • free(): Memory को deallocate करने के लिए
  • free(): To deallocate memory

Memory Leaks (मेमोरी लीक्स)

Memory leaks और उनसे बचने के तरीके:

Memory leaks and how to prevent them:

// Memory leak example
void memory_leak_example() {
    int *ptr = (int*)malloc(sizeof(int));
    *ptr = 10;
    // Memory leak: ptr not freed
}

// Correct memory management
void correct_memory_management() {
    int *ptr = (int*)malloc(sizeof(int));
    if (ptr == NULL) {
        printf("Memory allocation failed\n");
        return;
    }
    
    *ptr = 10;
    printf("Value: %d\n", *ptr);
    
    free(ptr);
    ptr = NULL;  // Prevent dangling pointer
}

Best Practices (सर्वोत्तम प्रथाएं)

Memory management के best practices:

Best practices for memory management:

  • Memory Allocation: हमेशा memory allocation check करें
  • Memory Allocation: Always check memory allocation
  • Memory Deallocation: Allocated memory को free करें
  • Memory Deallocation: Free allocated memory
  • Dangling Pointers: NULL pointers का use करें
  • Dangling Pointers: Use NULL pointers
  • Memory Leaks: Memory leaks को detect और fix करें
  • Memory Leaks: Detect and fix memory leaks