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를 할당하여야 할 것이다.

'Papers > programming' 카테고리의 다른 글

_msize  (0) 2008.03.13
The usage of a static class function by an object  (0) 2008.03.12
debugging problem  (0) 2008.02.26
class 상호 참조  (0) 2008.02.20
static class  (0) 2008.02.13