Dynamic memory allocation

 Dynamic memory allocation

Dynamic memory allocation is a process of allocating memory at runtime in a program. In C language, you can use the malloc() function to allocate memory dynamically. Here's a short example:



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

int main() {
    int *p;
    int n = 5;
    p = (int*) malloc(n * sizeof(int));
    
    if (p == NULL) {
        printf("Memory allocation failed.\n");
        exit(1);
    }
    
    for (int i = 0; i < n; i++) {
        p[i] = i+1;
    }
    
    for (int i = 0; i < n; i++) {
        printf("%d ", p[i]);
    }
    
    free(p);
    return 0;
}

In this example, we first declare a pointer variable p of type int. We then use the malloc() function to allocate memory dynamically for n integers. The size of the memory block allocated is n * sizeof(int). We cast the pointer returned by malloc() to (int*) to specify that we want a pointer to an integer.

We then check if p is NULL, which indicates that the memory allocation failed. If it did, we print an error message and exit the program.

Assuming the memory allocation succeeded, we then use a loop to initialize the memory block with the integers from 1 to n. Finally, we use another loop to print out the values of the integers.

When we are done using the memory block, we should release it using the free() function. This is important because failing to release dynamically allocated memory can cause memory leaks, which can eventually lead to program crashes or other issues.

Note that while this is a short example, it is important to be careful when using dynamic memory allocation, as it can lead to bugs if not used correctly. It is also important to note that dynamic memory allocation should only be used when necessary, as it can be less efficient than static memory allocation.

Allocating and reallocating memory:-

Allocating and reallocating memory in C language is done using the malloc() and realloc() functions, respectively. Here's a short example that demonstrates how to allocate and reallocate memory in C:

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

int main() {
    int *p;
    int n = 5;
    p = (int*) malloc(n * sizeof(int));
    
    if (p == NULL) {
        printf("Memory allocation failed.\n");
        exit(1);
    }
    
    for (int i = 0; i < n; i++) {
        p[i] = i+1;
    }
    
    for (int i = 0; i < n; i++) {
        printf("%d ", p[i]);
    }
    
    // Reallocate memory
    n = 10;
    p = (int*) realloc(p, n * sizeof(int));
    
    if (p == NULL) {
        printf("Memory reallocation failed.\n");
        exit(1);
    }
    
    for (int i = 5; i < n; i++) {
        p[i] = i+1;
    }
    
    for (int i = 0; i < n; i++) {
        printf("%d ", p[i]);
    }
    
    free(p);
    return 0;
}

In this example, we first allocate memory for n integers, just like in the previous example. We then initialize the memory block with the integers from 1 to n and print them out.

Next, we decide that we need more memory, so we use the realloc() function to increase the size of the memory block to hold 10 integers. We assign the returned pointer to p again. Note that if the reallocation fails, the realloc() function returns NULL. In that case, we print an error message and exit the program.

After reallocation, we use a loop to initialize the new memory block with the integers from 6 to 10, and print them out along with the original integers.

Finally, we release the dynamically allocated memory using the free() function.

Note that when reallocating memory, the contents of the original memory block are preserved if possible. However, if the new block is larger than the old block, the additional memory is uninitialized and may contain garbage values. Similarly, if the new block is smaller than the old block, some of the data may be lost.

Also note that it is important to be careful when reallocating memory, as it can lead to bugs if not used correctly. In particular, if you assign the returned pointer from realloc() to a different pointer variable, you may accidentally lose the reference to the original memory block, causing a memory leak.

allocating memory for structure and array:-

In C language, memory can be allocated dynamically using the malloc() function. To allocate memory for a structure or an array of short words, the size of each element must be taken into consideration.

Assuming a structure with two short word members and an array of 10 short words, the following code demonstrates how to allocate memory for them:

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

int main() {
    // Allocate memory for a structure with two short word members
    struct myStruct {
        short a;
        short b;
    };
    struct myStruct *ptr_struct = (struct myStruct*) malloc(sizeof(struct myStruct));

    // Allocate memory for an array of 10 short words
    short *ptr_array = (short*) malloc(10 * sizeof(short));

    // Use the allocated memory
    ptr_struct->a = 1;
    ptr_struct->b = 2;
    ptr_array[0] = 3;
    ptr_array[1] = 4;

    // Free the allocated memory when done using it
    free(ptr_struct);
    free(ptr_array);

    return 0;
}

In the above code, sizeof(struct myStruct) gives the size of the structure in bytes, which is equal to the size of two short words. Similarly, sizeof(short) gives the size of a short word in bytes. Therefore, to allocate memory for an array of 10 short words, we multiply the size of a short word by 10.

Note that it is important to cast the result of malloc() to the appropriate pointer type. Also, it is a good practice to check if malloc() returns NULL, which indicates that memory allocation has failed.






Post a Comment

Previous Post Next Post