代码如下: #include <stdio.h> #include <stdlib.h> int main(void) { char *str; int i; for(i=0;i<5;i++) { str = (char*)malloc(sizeof(char)*200000); if(str == NULL) { printf("malloc error!!!/n"); exit(-1); } sprintf(str,"Hello,World!"); printf("str is %s/n",str); free(str);//tag A } free(str); return 0; } 很显然,如果在代码tag A处没有释放内存的话,那势必造成内存不断的分配而无法释放,造成内存泄露。 一定要注意,malloc和free是作为一起出现的!