我有以下代码:
int main()
{
char * str1 = (char*)malloc(101 * sizeof(char));
for (int i=0; i<100; i++)
{
str1[i] = 'b';
}
str1[100] = 0;
char * str2 = (char*)malloc(1001 * sizeof(char));
for (int i=0; i<1000; i++)
{
str2[i] = 'a';
}
str2[1000] = 0;
for (int i=0; i<7000; i++)
{
char * tmp = str2;
str2 = (char*) malloc((strlen(str2) + strlen(str1) + 1) * sizeof(char));
sprintf(str2, "%s%s", tmp, str1);
free(tmp);
}
free(str1);
free(str2);
}
Run Code Online (Sandbox Code Playgroud)
运行时,任务管理器报告以下内存使用情况:程序开头 - 1056K,程序结束 - 17,748K
据我所知,没有内存泄漏,我编译它没有调试符号(发布模式). …