Programing - C/C Basic grammar
092 realloc 메모리 재할당
junmoyo
2017. 7. 28. 14:12
개념 :
malloc 이나 calloc 된 공간을 재 할당 할때 사용합니다.
소스코드 : main.c
#include <stdio.h>
#include <string.h>
#include <malloc.h>
int main(void)
{
char *pString = "Hello JunmoZzi";
char *pAdd = " ^^a ha ha ha!";
char *pBuffer = NULL;
int nSize1 = strlen(pString) + 1;
int nSize2 = strlen(pAdd) + 1;
pBuffer = calloc(nSize1 + 1, sizeof(char));
if (pBuffer == NULL)
{
puts("memory 할당 실패");
}
else
{
puts("memory 할당 성공");
printf("메모리 주소 : %d\n", pBuffer);
strcpy_s(pBuffer, nSize1, pString);
}
puts(pBuffer);
puts("메모리 재 할당");
realloc(pBuffer, nSize1 + nSize2);
strcat_s(pBuffer, nSize1 + nSize2, pAdd);
printf("메모리 주소 : %d\n", pBuffer);
puts(pBuffer);
free(pBuffer);
pBuffer = NULL;
return 0;
}
결과
#realloc,#c언어, #c언어입문, #프로그램입문, #realloc, #메모리재할당