【发布时间】:2011-01-14 22:42:21
【问题描述】:
看看刚刚提出的这个问题:Inconveniences of pointers to static variables 这样做会被认为是不好的做法吗?
char* strpart(char* string, int start, int count)
{
char* strtemp;
int i = 0; int j = 0;
int strL = strlen(string);
if ( count == 0 )
{
count = strL;
}
strtemp = (char*) calloc((count + 1), sizeof(char));
for ( i = start; i < (start+count); i++ )
{
strtemp[j] = string[i];
j++;
}
return strtemp;
}
抱歉,它写得很快,但基本原则是 - 当不在函数内使用静态缓冲区时,在函数内分配内存是不好的做法吗?我假设是因为它不会被释放,不是吗?不过我觉得我应该问一下。
【问题讨论】:
标签: c pointers memory-management calloc