Spread the post

Dynamic array memory allocation of C ?

C programming are provide following function to create dynamic memory allocation .

1) malloc: malloc function are use to create dynamic memory allocation of heap area. and there are allocated memory value is to Garbage value

Syntax of malloc()

2) calloc: calloc function are used to create dynamic memory allocation of heap area. And assign memory value is zero(0).

Syntax of calloc()

3) realloc: Change the size of previously allocated memory space.

Syntax of realloc()

4) free: Release the space of allocated memory.

Syntax of free()


Assume that create dynamic array of size 10. And assign array element

View structure of this process.

Stack Areamainarray(pointer)index (int) =10size (int) =10show_datasize (int) =10temp(pointer) Heap Areaarray (int) [0] 0 [1] 2 [2] 4 [3] 6 [4] 8 [5] 10 [6] 12 [7] 14 [8] 16 [9] 18

Below in C program create dynamic array using malloc function. And inserting array element.

View execution process.

Stack Areamainarray(pointer)index (int) =10size (int) =10show_dataindex (int) =3size (int) =10temp(pointer) Heap Areaarray (int) [0] 0 [1] 2 [2] 4 [3] 6 [4] 8 [5] 10 [6] 12 [7] 14 [8] 16 [9] 18

Visualize Code

Spread the post

Recommended Posts: