8. C Programming

Dynamic Memory Allocation

অ্যারে(Array) হ’ল একটি নির্দিষ্ট সংখ্যক মানের সংগ্রহকরন । একবার অ্যারের আকার ঘোষণা (Array Size Declaration) হয়ে গেলে আপনি এটিকে পরিবর্তন করতে পারবেন না।

কখনও কখনও আপনি ঘোষিত অ্যারের আকার(Array Size) অপর্যাপ্ত হতে পারে। এই সমস্যাটি সমাধান করার জন্য, Run Time -এ আপনি mannualy memory  allocation করতে পারেন। এটি  প্রোগ্রামিংয়ে (Programming) Dynamic Memory Allocation  নামে পরিচিত।

Dynamic Memory Allocation- এর জন্য  C Programming Language কতগুলি  Library Function ব্যবহার করা হয় এবং এগুলি “stdlib.h” নামক header file -এ defined করা থাকে। Library Function গুলি হল-

  • malloc() 
  • calloc()
  • realloc()
  • free

malloc() কি? উদাহারন সহযোগে ব্যাখ্যা কর ।

malloc() function-এর অর্থ হল- memory allocation- যা  নির্দিষ্ট সংখ্যক বাইটের মেমরির একটি ব্লক(Single block of memory) সংরক্ষণ করে।এটি void pointer return করে যার ফলে যে কোনও form-এর pointer-এ cast করা যায়। malloc() function ব্যবহার করলে মেমরি uninitialized অবস্থায় থাকে। 

Syntax: ptr= (castType*)malloc(size);

Example: ptr = (int*) malloc(10 * sizeof(int));

উপরিউক্ত উদাহারন থেকে বলতে পারি 10*4=40 byte size এর মেমরির একটি ব্লক বরাদ্দ (allocate) হয়েছে। এখানে বরাদ্দকৃত মেমরির প্রথম বাইট (First byte of allocated memory) -এর address কে point করে ptr .

Example :
#include <stdio.h>
#include <stdlib.h>

int main() {
    // Allocate memory for an array of 5 integers
    int *arr = (int *)malloc(5 * sizeof(int));

    // Check if memory allocation was successful
    if (arr == NULL) {
        fprintf(stderr, "Memory allocation failed. Exiting the program.\n");
        return 1;
    }

    // Initialize the array elements
    for (int i = 0; i < 5; ++i) {
        arr[i] = i * 2;
    }

    // Display the array elements
    printf("Array elements: ");
    for (int i = 0; i < 5; ++i) {
        printf("%d ", arr[i]);
    }

    // Free the dynamically allocated memory
    free(arr);

    return 0;
}

calloc() কি? উদাহারন সহযোগে ব্যাখ্যা কর ।

calloc() function-এর অর্থ হল- contiguous  allocation- যা  নির্দিষ্ট সংখ্যক বাইটের মেমরির অনেকগুলি ব্লক(multiple block of memory) সংরক্ষণ করে। calloc() function ব্যবহার করলে মেমরি প্রত্যেক টি বিট-এর মান শূন্য হয়ে যায়। 

Syntax: ptr= (castType*)calloc(n,size);

Example: ptr = (int*) calloc(5 ,sizeof(int));

উপরিউক্ত উদাহারন থেকে বলতে পারি মেমরিতে 5 ব্লক বরাদ্দ (allocate) হয়েছে যার প্রত্যেক টির আকার 5*4=20 byte ।

Example
int main() {
// Allocate memory for an array of 5 integers using calloc
        int *arr = (int *)calloc(5, sizeof(int));
// Check if memory allocation was successful
if (arr == NULL) {
    fprintf(stderr, "Memory allocation failed. Exiting the program.\n");
    return 1;
}

// Display the array elements (initialized to 0 by calloc)
printf("Array elements: ");
for (int i = 0; i < 5; ++i) {
    printf("%d ", arr[i]);
}
// Free the dynamically allocated memory
free(arr);
return 0;

}

realloc() কি? উদাহারন সহযোগে ব্যাখ্যা কর ।

realloc()- re- allocation অর্থাৎ malloc()/ calloc() দ্বারা পূর্বে বরাদ্দকৃত মেমরির পুনরায় বৃদ্ধি অথবা হ্রাস করা।

Syntax: ptr= realloc(ptr,size);

Example
#include <stdio.h>
#include <stdlib.h>

int main() {
    // Allocate memory for an array of 3 integers
    int *arr = (int *)malloc(3 * sizeof(int));

    // Check if memory allocation was successful
    if (arr == NULL) {
        fprintf(stderr, "Memory allocation failed. Exiting the program.\n");
        return 1;
    }

    // Initialize the array elements
    for (int i = 0; i < 3; ++i) {
        arr[i] = i * 2;
    }

    // Display the original array elements
    printf("Original array elements: ");
    for (int i = 0; i < 3; ++i) {
        printf("%d ", arr[i]);
    }

    // Resize the array to hold 5 integers using realloc
    arr = (int *)realloc(arr, 5 * sizeof(int));

    // Check if reallocation was successful
    if (arr == NULL) {
        fprintf(stderr, "Memory reallocation failed. Exiting the program.\n");
        return 1;
    }

    // Initialize the additional elements
    for (int i = 3; i < 5; ++i) {
        arr[i] = i * 2;
    }

    // Display the updated array elements
    printf("\nUpdated array elements: ");
    for (int i = 0; i < 5; ++i) {
        printf("%d ", arr[i]);
    }

    // Free the dynamically allocated memory
    free(arr);

    return 0;
}

free() কি? উদাহারন সহযোগে ব্যাখ্যা কর ।

free() -এই function টি ব্যবহার করলে malloc()/calloc() দ্বারা বরাদ্দকৃত মেমরি মুক্ত হয়।

Syntax: free(ptr);

উপরের উদাহারন গুলি তে উল্লেখ করা হয়েছে  free() এর ব্যবহার ।

Leave a comment