문자열 저장이 가능한 배열 malloc 예시
char * str = (char*)malloc(sizeof(char)*len);
위와 같이 선언하면 heap영역에 메모리를 동적할당 할 수 있다.
c++에서는 new 키워드를 통해 동적할당을 더 쉽게 할 수 있다.
`char * str = new char[len];
- (char*)같은 형변환이 필요없다.
- 바이트 단위로 할당할 메모맄크기를 넘길 필요가 없다.
int* num1 = new int;
-> int형 변수 할당
int* num2 = new int[10];
-> 크기가 10인 int형 배열 할당
delete num1;
-> 메모리 해제
delete []num2;
-> 배열형태 메모리 해제
int * num1 = new int;
int &ref = *num1;
-> 동적할당 된 메모리도 변수로 간주되 참조자로 참조할 수 있다.