본문 바로가기

알고리즘/C++

C++ 배열 동적할당

1차원 배열 동적할당

int n=7;

// 메모리 할당
int *arr = new int[n];

// 할당 메모리 삭제
delete[] arr;

 

2차원 배열 동적할당

int row=6, col=13;

// 메모리 할당
int **mat;
mat = new int*[row];
for (int i=0; i<row; i++)
	mat[i] = new int[col];

// 할당 메모리 삭제
for (int i=0; i<row; i++)
	delete [] mat[i];
delete [] mat;

'알고리즘 > C++' 카테고리의 다른 글

3. 스택 (Stack)  (0) 2020.07.21
Container Class와 Template  (0) 2020.07.13
2. 배열 (Array)  (0) 2020.07.13
1. 기본 개념  (0) 2020.07.12
C 배열 동적할당  (0) 2020.06.21