Papers/programming
[debug problem] memory allocation
tomato13
2008. 3. 4. 21:03
void TC10()
{
char aStr[5] = "test"; // (1)
int nLen = strlen(aStr);
char* pStr = (char*) malloc(sizeof(char)*nLen); // (2)
strcpy(pStr, aStr);
free(pStr);
}
위의 코드를 compile하면 error가 발생한다.(당연한가..ㅡ.ㅡ;;) (1)에서는 마지막 문자로 NULL을 넣어주어야하면 (2)에서도 이를 고려하여 string length+1을 넣어 memory를 할당하여야 할 것이다.