Dynamic Memory Allocation

추워요·2023년 11월 17일

영어연습

목록 보기
1/1

원본

Concept

Dynamic memory allocation means allocating memories when the program is running.

Characteristics

It can allocate desired memory space dynamically.
The memory should be freed manually.
The memory allocation is maintained unless the program is finished.

Syntax

  • Stdlib.h library should be included.

    #include <stdlib.h>

  • Typically, malloc function is used to allocate memory.

    malloc(memory size);

    Memory size can be easily obtaioned by sizeof(type) * array size.

  • Free the memory by using free() function.

    free(ptr);

    Calloc function also can be used to allocate memory.

    calloc(size, size of type);

    Calloc initializes the value to 0 that the pointer points when declared.

  • Reallocate the memory by using realloc() function.

    realloc(ptr, memory size);

  • Specify the type of dynamically allocated arrays.

    a = (int*) malloc(sizeof(int) * 10);

    The return type of malloc, calloc is void*.
    So, when initializing a pointer, the type of pointer should be specified.

Array and pointer

Arrays and pointers can be used interchangably.

Declarization of arrays and pointers

int arr[10];

int* arr;
arr = (int*) malloc(sizeof(int) * 10);

Allocation the memory of array


When an array is declared, the memory is allocated like this.

Let's see the code.

#include <stdio.h>

void main()
{
	int arr[5];
	for (int i = 0; i < 5; i++)
	{
		printf("%p\n", &arr[i]);
	}
}

출력

000000E57D3FFA98
000000E57D3FFA9C
000000E57D3FFAA0
000000E57D3FFAA4
000000E57D3FFAA8

It is difficult to understand because it is hexadecimal, you can see the differences between the addresses are 4.
It is because the size of int is 4 bytes.
Differences between memory addresses will equal to the size of array type.

Array is same as pointer

Let's execute the code below.

#include <stdio.h>

void main()
{
	int arr[5];
	printf("%p %p", arr, &arr[0]);
}

output

000000BBA1BFF9B8 000000BBA1BFF9B8

According to the output, you can see the value of arr is same as &arr[0].
It means arr is pointer of arr[0].

Then, if add 4 to arr, that will be &arr[0] (refer to Allocation the memory of array)

Let' see.

#include <stdio.h>

void main()
{
	int arr[5];
	printf("%p %p", arr+4, &arr[1]);
}

output

0000002DC61FF928 0000002DC61FF91C

It is different from previous prediction.

It is because in arrays or pointers, + operator moves as much as the size of array type.
Therefore, +4 does not means +4 of memory adress, but +4 of array compartments.

So if correct to +1,

#include <stdio.h>

void main()
{
	int arr[5];
	printf("%p %p", arr+1, &arr[1]);
}

output

000000A78FDFF86C 000000A78FDFF86C

You can see both are same.

Implementation of multidimentional array

If arrays and pointers are same, we can implement multidimentional array by using a pointer.

First of all, let's see how the 2 dimentional array is implemented in memory.

#include <stdio.h>

void main()
{
	int arr[3][3];
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			printf("%p ", &arr[i][j]);
		}
		printf("\n");
	}
}

output

000000D1A49FF8C8 000000D1A49FF8CC 000000D1A49FF8D0
000000D1A49FF8D4 000000D1A49FF8D8 000000D1A49FF8DC
000000D1A49FF8E0 000000D1A49FF8E4 000000D1A49FF8E8

Following is made easier to read(last 2 digits are converted to decimal).

Therefore, it can be seen that a 2 dimentional array is implemented like this.

If implement it using pointer, it will be the following form.

Through the code below, we can declare a 2 dimentional array using pointer.

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

void main()
{
	int** a;

	a = (int**)calloc(3,sizeof(int*));
	for (int i = 0; i < 5; i++)
	{
		a[i] = (int*)calloc(5, sizeof(int));
	}
	
	for (int i = 0; i < 5; i++)
	{
		free(a[i]);
	}
	free(a);
}

However, if a 2 dimentional array is declared dynamically, the memory structure have the following form.

int a[3][5];

If an array is declared like this, the adresses of a[0], a[1], a[2] are consecutive.
But if an array is declared dynamically, because malloc or calloc function allocates memory adrresses randomly, &a[0], &a[1], &a[2] are not consecutive.

Free memory of multidimentional array.

Assembly is reverse of disassembly.
As you can see the code above, we can see free a[i] first that is allocated lastly, then free a.
This way have to free memories for each dementions.

int* a = (int*)malloc(15 * sizeof(int));

If declare a pointer like above and use like 3 X 5 array, we can free the memory at once.
But it requires manual index calculation. (Access is impossible like a[1][2])

Desired 원하는
Initialization 초기화
Declare 선언
Specify 지정하다
Interchangable 혼용할 수 있다.
Difference between A and B A와 B의 차
Execute 실행하다
Refer to 참고하다
As much as 만큼
Compartment 칸
It can be seen 로 볼 수 있다.
Implement 구현
Consecutive 연속적인
At once 한번에

profile
차근차근 배우는 중인 대학생

0개의 댓글