[C] Heap & Dynamic memory allocation

2 minute read

In the previous post, we looked at the array form that is declared and used with a fixed size variable type. However, the static memory allocation method, which uses a certain amount of memory by determining the size of an array, wastes memory in that it must be used while maintaining a limited size. Dynamic memory allocation uses the Heap area to allocate and release the memory area, which enables efficient system memory management.

A pointer variable is used to dynamically allocate memory through the Heap area. As we saw in the articles related to pointer, there was a method of assigning an address value when initializing after declaring a variable, or referencing the address where the value was recorded and transmitting it. For dynamic memory allocation, declare how many memory areas will be used instead of the above two methods. The functions used at this time are functions such as malloc, calloc, realloc, and free, and are stored in stdlib.h.

Name Expression
malloc void * malloc (size)
calloc void * calloc (number, size)
realloc void * realloc (void * ptr, size)
free void free (void *ptr)

When looking at functional types, all of them are void, so when assigning to a variable, the variable type must be matched through casting. First, in the case of malloc, the size of the memory area to be allocated is passed as an argument, and in the case of calloc, the number of variables and the unit size to be used are used as inputs, and allocated memory equal to number * size is allocated. In the case of realloc, a function that reallocates a memory area can reduce, enlarge, and return the area.

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

int *a;
int *b;

int main(){
    a = (int*)malloc(4*5);
    if(a==0x00) return 0;

    for(int i=0; i<5; i++){
        *(a+i)=i;
        printf("%d ", *(a+i));
    }

    b = (int*)malloc(5 * sizeof(int));

    if(b==NULL) return 0;

    for(int i=0; i<5; i++){
        b[i]=i;
        printf("%d ", b[i]);
    }
    free(a);
    free(b);
    return 0;
}
0 1 2 3 4 0 1 2 3 4
#include <stdio.h>
#include <stdlib.h>
int *p;

int main(){
    p = (int *)malloc (5 * sizeof(int));
    for(int i=0; i<5; i++){
        p[i]=i;
        printf("%d ", p[i]);
    }

    p= (int*)realloc(p, 10*sizeof(int));

    for(int i=5; i<10; i++){
        p[i]=i;
        printf("%d ", p[i]);
    }

    realloc(p, 0);
    return 0;
}

0 1 2 3 4 5 6 7 8 9

Just as there were two-dimensional arrays in array, dynamic memory allocation using pointers can also implement two-dimensional array. It uses double pointer and uses a loop to allocate dynamic memory. Allocates memory corresponding to the vertical length of array to the first double pointer. Afterwards, by allocating memory equal to the horizontal length to each declared pointer array, an array of depth 2 or 2-dimensional can be declared. Afterwards, the pointer performs a for loop as long as the row length to return the dynamic memory, returns the column-oriented memory, and returns the dynamic memory of the double pointer.

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

int **a;

int main()
{
    **a = (int**)malloc(sizeof(int *) * 2);   

    for (int i = 0; i < 2; i++){
        a[i] = malloc(sizeof(int) * 2);   
    }

    a[0][0] = 1;  
    a[0][1] = 2;
    a[1][0] = 3;    
    a[1][1] = 4;   

    printf("%d ", a[0][0]);
    printf("%d ", a[0][1]);
    printf("%d ", a[1][0]);
    printf("%d ", a[1][1]);

    for (int i = 0; i < 2; i++){
        free(a[i]);               
    }

    free(a);   
    return 0;
}
1 2 3 4