반응형
//******************************************************** // 동적 배열 //******************************************************* // 1) C언어 !! // --> malloc, calloc , realloc //******************************************************** //******************************************************** #include#include void printArray( int * buf ) { for(int i = 0; i< _msize(buf)/sizeof(int) ; i++) { printf("%d", *(buf+i)); } puts(""); } void main() { // malloc --> heap메모리에 내가 원하는 만큼의 메모리 공간을 할당하고 // 공간의 시작 주소값을 리턴해주는 함수 !! //int * buf = (int*)malloc( sizeof( int) * 10 ); // malloc + memset // int * buf = (int*)calloc( 10, sizeof(int)); // _msize : heap에 할당된 메모리의 크기 /* printf("buf : %d\n" , _msize( buf)); printArray( buf); buf = (int*)realloc(buf, _msize(buf)*2); printArray( buf); printf("buf : %d\n" , _msize( buf)); free( buf); */ int * buf = new int[10]; printf("buf : %d\n" , _msize( buf)); // 메모리 해제 !! delete [] buf; }
'자료구조' 카테고리의 다른 글
[자료구조] 배열을 추상화 한 클래스 (0) | 2014.12.01 |
---|---|
[자료구조] C언어 동적배열 사용 (0) | 2014.11.18 |
[자료구조] C 언어 정적배열 (0) | 2014.11.18 |
[자료구조] 포인터 연산과 다차원 배열 (0) | 2014.11.18 |
[자료구조] 포인터와 배열 (0) | 2014.11.18 |