user-0 提问: 诸神请看：第二次调用malloc给结构指针分配空间时出错  0xC0000374: 堆已损坏。

回复:
user: user-1
content: 仅供参考//将c:\\tmp文件夹下的所有文件的内容全部放到用malloc分配的内存中 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <io.h> struct FB {     char fn[256];     size_t fl;     char *b;     struct FB *next;     struct FB *prev; } *fh,*fb,*ft; char ln[256]; char fpn[256]; FILE *af; FILE *f; int L,n; int main() {     system("dir /b /a-d c:\\tmp\\*.* >c:\\allfn.txt");     af=fopen("c:\\allfn.txt","r");     if (NULL==af) {         printf("Can not open file c:\\allfn.txt!\n");         return 1;     }     fh=NULL;     fb=NULL;     n=0;     while (1) {         if (NULL==fgets(ln,256,af)) break;         L=strlen(ln);         if ('\n'==ln[L-1]) ln[L-1]=0;         printf("read %s\n",ln);         strcpy(fpn,"c:\\tmp\\");         strcat(fpn,ln);         ft=(struct FB *)malloc(sizeof(struct FB));         if (NULL==ft) {             printf("Can not malloc ft!\n");             fclose(af);             return 2;//之前的malloc在main退出后由操作系统自动free         }         printf("ft[%d]==%p\n",n,ft);         strcpy(ft->fn,fpn);         f=fopen(fpn,"rb");         if (NULL==f) {             printf("Can not open file %s!\n",fpn);             fclose(af);             return 3;//之前的malloc在main退出后由操作系统自动free         }         ft->fl=_filelength(fileno(f));         ft->b=malloc(ft->fl);         if (NULL==ft->b) {             printf("Can not malloc ft->b!\n");             fclose(f);             fclose(af);             return 4;//之前的malloc在main退出后由操作系统自动free         }         printf("ft[%d]->b==%p\n",n,ft->b);         if (ft->fl!=fread(ft->b,1,ft->fl,f)) {             printf("fread error!\n");             fclose(f);             fclose(af);             return 5;//之前的malloc在main退出后由操作系统自动free         }         fclose(f);         ft->next=NULL;          if (NULL==fh) {             ft->prev=NULL;             fh=ft;         } else {             fb->next=ft;             ft->prev=fb;         }         fb=ft;         n++;     }     fclose(af);     printf("-----list-----\n");     for (ft=fh;NULL!=ft;ft=ft->next) {         printf("%8d %s\n",ft->fl,ft->fn);         if (NULL!=ft) fb=ft;     }     printf("-----free-----\n");     n--;     if (NULL!=fh) {         for (ft=fb->prev;NULL!=ft;ft=ft->prev) {             if (NULL!=ft->next->b) {                 printf("ft[%d]->b==%p\n",n,ft->next->b);                 free(ft->next->b);             }             if (NULL!=ft->next) {                 printf("ft[%d]==%p\n",n,ft->next);                 free(ft->next);             }             n--;         }         if (NULL!=fh->b) {             printf("ft[0]->b==%p\n",fh->b);             free(fh->b);         }         printf("ft[0]==%p\n",fh);         free(fh);     }     return 0; } //C:\tmp\tmp\Debug>dir /a-d c:\tmp // 驱动器 C 中的卷是 C_HD5_1 // 卷的序列号是 1817-D526 // // c:\tmp 的目录 // //找不到文件 // //C:\tmp\tmp\Debug>tmp //找不到文件 //-----list----- //-----free----- // //C:\tmp\tmp\Debug>dir /a-d c:\tmp // 驱动器 C 中的卷是 C_HD5_1 // 卷的序列号是 1817-D526 // // c:\tmp 的目录 // //2011-06-30  18:04            44,840 my_c.rar //2011-06-30  17:18             1,036 err.frm //2011-06-30  14:32            14,243 出租.txt //2011-06-28  12:08            23,681 MSDN98书签.txt //             4 个文件         83,800 字节 //             0 个目录 17,041,870,848 可用字节 // //C:\tmp\tmp\Debug>tmp //read my_c.rar //ft[0]==00421800 //ft[0]->b==00520068 //read err.frm //ft[1]==00421670 //ft[1]->b==0052AFC0 //read 出租.txt //ft[2]==00421530 //ft[2]->b==00378F28 //read MSDN98书签.txt //ft[3]==004213F0 //ft[3]->b==0052B3F8 //-----list----- // 44840 c:\tmp\my_c.rar //  1036 c:\tmp\err.frm // 14243 c:\tmp\出租.txt // 23681 c:\tmp\MSDN98书签.txt //-----free----- //ft[3]->b==0052B3F8 //ft[3]==004213F0 //ft[2]->b==00378F28 //ft[2]==00421530 //ft[1]->b==0052AFC0 //ft[1]==00421670 //ft[0]->b==00520068 //ft[0]==00421800 // //C:\tmp\tmp\Debug>

user: user-0
content: 引用 6 楼 zhao4zhong1 的回复:函数shuzhuangfuzhi改为： struct node *shuzhuangfuzhi(struct node *p, char *word,int weizhi) {     int a;     if (p == NULL) {          p = talloc();         p->biaozhi = 0;         p->p = strup(word);         p->zuo = NULL;         p->you = NULL;     } else if ((a = bijiao(p->p, word)) > 0) {         if (a > weizhi)p->biaozhi = 0;         else p->biaozhi = 1;         p->zuo=shuzhuangfuzhi(p->zuo, word, weizhi);     } else if (a  < 0) {         if (a > weizhi)p->biaozhi = 0;         else p->biaozhi = 1;         p->you=shuzhuangfuzhi(p->you, word, weizhi);     }     return p; }  试试看。不保证对。 我改过了，还是没有用~~~~ 问题我还没找到，我用其他方式代替了，我写了一个包含这个错误的很简单的程序，如果您有时间的话可以试着找找原因，找到了请告诉我一声，非常感谢。 #include<stdio.h> #include <ctype.h> #define MAXLEN 1000 #include <string.h> #include <stdlib.h>  struct node {     int biaozhi;     char *p;     struct node *zuo;     struct node *you; }; int shuzhuangfuzhi(struct node *, char *word); int main() {     struct node *p = NULL;     int  c = 0,i=0;     char word[MAXLEN];     char *fuzhi[MAXLEN] = {"hello","man",NULL};     while (fuzhi[i]!=NULL)     {     p = shuzhuangfuzhi(p, fuzhi[i++]);     }      system("pause"); }   struct node *talloc(void); char *strup(char *);  int shuzhuangfuzhi(struct node *p, char *word) {     int a;     if (p == NULL)     {          p = talloc();         p->biaozhi = 0;         p->p = strup(word);         p->zuo = NULL;         p->you = NULL;     }     else          shuzhuangfuzhi(p->zuo, word);     return p; }  struct node *talloc(void) {     return (struct node *) malloc(sizeof(struct node)); }   char *strup(char *s) {     char *p;     p = (char *)malloc(strlen(s) + 1);     if (p != NULL)         strcpy_s(p, 50, s);     return p; }

user: user-1
content: 函数shuzhuangfuzhi改为： struct node *shuzhuangfuzhi(struct node *p, char *word,int weizhi) {     int a;     if (p == NULL) {          p = talloc();         p->biaozhi = 0;         p->p = strup(word);         p->zuo = NULL;         p->you = NULL;     } else if ((a = bijiao(p->p, word)) > 0) {         if (a > weizhi)p->biaozhi = 0;         else p->biaozhi = 1;         p->zuo=shuzhuangfuzhi(p->zuo, word, weizhi);     } else if (a  < 0) {         if (a > weizhi)p->biaozhi = 0;         else p->biaozhi = 1;         p->you=shuzhuangfuzhi(p->you, word, weizhi);     }     return p; }  试试看。不保证对。

user: user-0
content: 整个程序如下  #include<stdio.h> #include <ctype.h> #define MAXLEN 1000 #include <string.h> #include <stdlib.h>  struct node { 	int biaozhi;     char *p; 	struct node *zuo; 	struct node *you; }; int shuzhuangfuzhi(struct node *, char *word,int weizhi);//返回一个正负数，正负号表示大于或小于，位数表示具体在哪一位出现的不相等 int getword(char *word, int lim); int bijiao(char *p, char *q); int bijiao1(char *p[], char *q); void printree(struct node *); int main(int argc, char *argv[]) { 	struct node *p=NULL; 	char *duizhao[] = { "void", "int", "char", "float", "double", "unsigned", "signed",NULL }; 	int weishu,a=0,b=0,c=0; 	if (argc == 1)weishu = 6; 	else if (argc > 2)printf("wrong use \n"); 	else { 		++argv; 		while ((a = *(*argv)++) != ' ') 			if (a<'0'&&a>'9') 			{ printf("wrong usage"); 		      return -1; 			} 			else b = b * 10 + a - '0'; 			weishu = b; 		} 	char word[MAXLEN]; 	while (getword(word, MAXLEN) != EOF) 	{ 			if (bijiao1(duizhao, word) == 0) 			{ 			while ((c = getword(word, MAXLEN) )!= ';'&& bijiao1(duizhao, word) != 0) 				if (isalpha(c)) 					p=shuzhuangfuzhi(p, word, weishu); 			} 	}  	printree(p); 	system("pause"); } void printree(struct node *p) { 	if (p != NULL) 	printree(p->zuo); 	if (p->biaozhi==1) 	printf("\n%s  ", p->p); 	else printf("%s  ", p->p); 	if (p->you!=NULL) 	printree(p->you);   } struct node *talloc(void); char *strup(char *);  int shuzhuangfuzhi(struct node *p, char *word,int weizhi) { 	int a; 	if (p == NULL) 	{ 		 		p = talloc(); 		p->biaozhi = 0; 		p->p = strup(word); 		p->zuo = NULL; 		p->you = NULL; 	} 	else if ((a = bijiao(p->p, word)) > 0) 	{ 		if (a > weizhi)p->biaozhi = 0; 		else p->biaozhi = 1; 		shuzhuangfuzhi(p->zuo, word, weizhi); 	} 	else if (a  < 0) 	{ 		if (a > weizhi)p->biaozhi = 0; 		else p->biaozhi = 1; 		shuzhuangfuzhi(p->you, word, weizhi); 	} 	return p; }  int bijiao(char *p, char *q) { 	int a=0; 	while (*(p + a) == *(q + a) && *(p + a)!='\0') 		a++; 	a += 1; 	if (*(p + a) == *(q + a)) 		return 0; 	if (*(p + a) > *(q + a)) 		return a; 	if (*(p + a) < *(q + a)) 		return -a; } int bijiao1(char *p[], char *q) { 	while (*p != NULL) 	{ 		if (strcmp(*p++, q) == 0) 			return 0; 	} 	return -1; } int getword(char *word, int lim) { 	int c, getch(void); 	void ungetch(int); 	char *w = word; 	while (isspace(c = getch())); 	if (c != EOF) 		*w++ = c; 	if (!isalpha(c)) 	{ 		*w = '\0'; 		return c;  	} 	for (; --lim > 0;w++) 		if (!isalnum(*w = getch())) 		{ 		ungetch(*w); 		break; 		} 	*w = '\0'; 	return word[0]; }  #define BUFSIZE 10000  char buf[BUFSIZE]; int bufp = 0; int getch(void) { 	return (bufp > 0) ? buf[--bufp] : getchar(); }   void ungetch(int c) { 	if (bufp >= BUFSIZE) 		printf("ungetch:too many"); 	else 		buf[bufp++] = c; }      struct node *talloc(void) { 	return (struct node *) malloc(sizeof(struct node)); }   char *strup(char *s) { 	char *p; 	p = (char *)malloc(strlen(s) + 1); 	if (p!= NULL) 		strcpy_s(p,50,s); 	return p; }

user: user-0
content: 引用 4 楼 zhao4zhong1 的回复:[quote=引用 3 楼 zhao4zhong1 的回复:] 函数shuzhuangfuzhi应该返回a吧。 或者返回p，但函数应返回struct node *类型吧。[/quote] 谢谢，目前我通过struct node *p[]代替每次对malloc（）的调用来分配内存地址，然后可以成功的运行了。 指出的问题确实是一个错误，我把int shuzhuangfuzhi 改为struct node *shuzhuangfuzhi，shuzhuangfuzhi(p->zuo, word, weizhi);改为p->zuo=shuzhuangfuzhi(p->zuo, word, weizhi);把shuzhuangfuzhi(p->you, word, weizhi);改为p->you=shuzhuangfuzhi(p->you, word, weizhi);还是出现了上述问题。以下是我成功运行的程序： //我要做的编写一个程序，读入一个C语言程序，并按字母顺序分组打印变量名，要求每一组内各变量名的前6个字符相同 //其余字符不同，将6作为一个可在命令行中设定的参数    #include<stdio.h> #include <ctype.h> #define MAXLEN 1000 #include <string.h> #include <stdlib.h>  struct node { 	char *p; 	struct node *zuo; 	struct node *you; };//采用树状结构存取变量     int shuzhuangfuzhi(struct node *, char *word, int weizhi, struct node *pn[]);//该函数用来排序，将新得到的单词和已有结构中的单词相比较， //单词小于已有单词，则传入结构中的*zuo指针中，之后继续比较，若大于则传入*you指针中，如相等则不进行任何操作，通过这种方式进行树状排序 int getword(char *word, int lim);//读入一个单词 int bijiao(char *p, char *q);//返回一个正负数，正负号表示大于或小于，位数表示具体在哪一位出现的不相等 int bijiao1(char *p[], char *q);//用来将所得的单词和已设变量名相比较，如单词和某一个变量相等，则返回0，否则返回-1 void printree(struct node *, int weishu);//用来打印排序好的树状结构中的字符串 int main(int argc, char *argv[]) { 	struct node *budeyi[100]; 	int ab = 0; 	for (ab = 0; ab < 20; ab++) 	{ 		budeyi[ab] = (struct node *) malloc(sizeof(struct node)); 	} 	char *pk[1000]; 	for (ab = 0; ab < 100; ab++) 	{ 		pk[ab] = (char *)malloc(sizeof(char *)); 	}   	struct node *p = NULL; 	char *duizhao[] = { "void", "int", "char", "float", "double", "unsigned", "signed", NULL }; 	int weishu, a = 0, b = 0, c = 0; 	if (argc == 1)weishu = 6; 	else if (argc > 2)printf("wrong use \n"); 	else { 		++argv; 		while ((a = *(*argv)++)) 			if (a<'0'&&a>'9') 			{ 			printf("wrong usage"); 			return -1; 			} 			else b = b * 10 + a - '0'; 			weishu = b; 	} 	char word[MAXLEN]; 	while (getword(word, MAXLEN) != EOF) 	{ 		if (bijiao1(duizhao, word) == 0) 		{ 			while ((c = getword(word, MAXLEN)) != ';'&& bijiao1(duizhao, word) != 0) 				if (isalpha(c)) 					p = shuzhuangfuzhi(p, word, weishu, budeyi, pk); 		} 	}  	printree(p, weishu); 	printf("\n"); 	system("pause"); }   char last[] = { "" }; void printree(struct node *p, int weishu) { 	int a; 	if (p != NULL) 	{ 		printree(p->zuo, weishu); 		if ((a = bijiao(last, p->p)) < 0) 			a = -a; 		if (a > weishu) 			printf("%s  ", p->p); 		else 			printf("\n%s   ", p->p); 		strcpy_s(last, 50, p->p); 		if (p->you != NULL) 			printree(p->you, weishu); 	}  } struct node *talloc(void); char *strup(char *);  int shuzhuangfuzhi(struct node *p, char *word, int weizhi, struct node *pn[], char *pl[]) { 	int a; 	if (p == NULL)//将所得变量名放入结构中，若结构指针为null，则说明此节点的字符指针没有赋值，则将变量名放入字符指针p->中 	{  		p = talloc(pn);//若之前结构指针p为null，则调用talloc函数为其分配内存地址，###我就是在第一次调用talloc函数时，给我分配了指针地址，当以第二次调用talloc（即读入的第三个字符）时， 		//没有给我分配指针地址。 		p->p = strup(word, pl); 		p->zuo = NULL; 		p->you = NULL; 	} 	else if ((a = bijiao(p->p, word)) > 0)//若该结构指针不是NULL说明此节点的字符指针已赋值，则将变量名与字符指针中的字符进行比较，若大于，则将 		//变量名与右节点即*you结构指针进行比较 	{ 		p->zuo = shuzhuangfuzhi(p->zuo, word, weizhi, pn, pl); 	} 	else if (a  < 0)//若小于，变量名与左节点即*zuo结构指针进行比较 	{ 		p->you = shuzhuangfuzhi(p->you, word, weizhi, pn, pl); 	} 	return p;//将变量地址返回 }  int bijiao(char *p, char *q) { 	int a = 0; 	while (*(p + a) == *(q + a) && *(p + a) != '\0') 		a++; 	if (*(p + a) == *(q + a)) 		return 0; 	if (*(p + a) > *(q + a)) 		return a + 1; 	if (*(p + a) < *(q + a)) 		return -(a + 1); } int bijiao1(char *p[], char *q) { 	while (*p != NULL) 	{ 		if (strcmp(*p++, q) == 0) 			return 0; 	} 	return -1; } int getword(char *word, int lim) { 	int c, getch(void); 	void ungetch(int); 	char *w = word; 	while (isspace(c = getch())); 	if (c != EOF) 		*w++ = c; 	if (!isalpha(c)) 	{ 		*w = '\0'; 		return c;  	} 	for (; --lim > 0; w++) 		if (!isalnum(*w = getch())) 		{ 		ungetch(*w); 		break; 		} 	*w = '\0'; 	return word[0]; }  #define BUFSIZE 10000  char buf[BUFSIZE]; int bufp = 0; int getch(void) { 	return (bufp > 0) ? buf[--bufp] : getchar(); }   void ungetch(int c) { 	if (bufp >= BUFSIZE) 		printf("ungetch:too many"); 	else 		buf[bufp++] = c; }    int m = 0;  struct node *talloc(struct node *p[]) { 	return p[m++]; }  int q = 0; char *strup(char *s, char *ph[]) { 	char *p; 	p = ph[q++]; 	if (p != NULL) 		strcpy_s(p, 50, s); 	return p; }

user: user-1
content: 引用 3 楼 zhao4zhong1 的回复:函数shuzhuangfuzhi应该返回a吧。 或者返回p，但函数应返回struct node *类型吧。

user: user-1
content: 函数shuzhuangfuzhi应该返回a吧。

user: user-1
content: 崩溃的时候在弹出的对话框按相应按钮进入调试，按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处，看不懂时双击下一行，直到能看懂为止。