You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
LearnCCpp/basic/4_memoryManagement.c

32 lines
602 B

#include <stdio.h>
#include <stdlib.h>
int main()
{
const int COUNT = 5;
// 동적 메모리 할당, ptr에 동적으로 할당된 메모리의 시작주소 저장
int *ptr = (int*)malloc(sizeof(int) * COUNT);
if (!ptr)
{
printf("Fail to malloc\n");
return 1;
}
for (int i = 0; i < COUNT; i++)
{
ptr[i] = i * 10; // 값 초기화
printf("%d ", ptr[i]); // 값 출력
}
printf("\n");
free(ptr);
printf("\n");
for (int i = 0; i < COUNT; i++)
{
printf("%d ", ptr[i]); // 값 출력
}
return 0;
}