| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 5034 人关注过本帖
标题:诸神请看:第二次调用malloc给结构指针分配空间时出错 0xC0000374: 堆已损坏 ...
只看楼主 加入收藏
zwdbxlt
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2015-1-1
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:13 
诸神请看:第二次调用malloc给结构指针分配空间时出错 0xC0000374: 堆已损坏。
大家新年好
我在做C程序设计语言的结构习题时,当我第2次条用malloc为指向结构的指针分配地址时出现错误,网上查了好久没找到答案,还请诸神帮我解决,谢谢。
图片附件: 游客没有浏览图片的权限,请 登录注册

图片附件: 游客没有浏览图片的权限,请 登录注册

图片附件: 游客没有浏览图片的权限,请 登录注册
搜索更多相关主题的帖子: C程序设计 空间 网上 
2015-02-20 12:14
zwdbxlt
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2015-1-1
收藏
得分:0 
整个程序如下

#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;
}
2015-02-20 12:15
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:10 
這樣命名的標識符,都不知道怎麽看,也不知道你的程序想幹什麽。有錯誤,把出錯的細節描述清楚,你說第二次分配,怎麽來的第二次?用了命令行參數輸入,你也不把命令行輸入了什麽就問人。這些都是該你自己替別人想好了,人家一定會問的。

不要真以爲“代碼是一切”,把代碼貼上來不等於什麽都不用說了。

授人以渔,不授人以鱼。
2015-02-20 13:33
zwdbxlt
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2015-1-1
收藏
得分:0 
回复 3楼 TonyDeng
//版主说的对,我要做的编写一个程序,读入一个C语言程序,并按字母顺序分组打印变量名,要求每一组内各变量名的前6个字符相同
//其余字符不同,将6作为一个可在命令行中设定的参数



#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);//该函数用来排序,将新得到的单词和已有结构中的单词相比较,
//单词小于已有单词,则传入结构中的*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 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;//没有输入命令行则默认为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;
    }//以上为获取命令行参数我这里没有输入命令行参数,即默认为6位
    char word[MAXLEN];
    while (getword(word, MAXLEN) != EOF)//读入一个单词
    {
        if (bijiao1(duizhao, word) == 0)//用来将所得的单词和已设变量名(即 int void char 等)相比较,若相等则执行以下代码
        {
            while ((c = getword(word, MAXLEN)) != ';'&& bijiao1(duizhao, word) != 0)//用来获取int,char,void等后面的单词,此单词即为变量
                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)//将所得变量名放入结构中,若结构指针为null,则说明此节点的字符指针没有赋值,则将变量名放入字符指针p->p中
    {

        p = talloc();//若之前结构指针p为null,则调用talloc函数为其分配内存地址,###我就是在第一次调用talloc函数时,给我分配了指针地址,当以第二次调用talloc(即读入的第三个字符)时,
        //没有给我分配指针地址。
        p->biaozhi = 0;
        p->p = strup(word);
        p->zuo = NULL;
        p->you = NULL;
    }
    else if ((a = bijiao(p->p, word)) > 0)//若该结构指针不是NULL说明此节点的字符指针已赋值,则将变量名与字符指针中的字符进行比较,若大于,则将
        //变量名与右节点即*you结构指针进行比较
    {
        if (a > weizhi)p->biaozhi = 0;
        else p->biaozhi = 1;
        shuzhuangfuzhi(p->zuo, word, weizhi);
    }
    else if (a  < 0)//若小于,变量名与左节点即*zuo结构指针进行比较
    {
        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;
}

[ 本帖最后由 zwdbxlt 于 2015-2-20 16:35 编辑 ]
2015-02-20 16:33
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
你這代碼連編譯都不能通過,整形和指針混用,什麽編譯環境通過了的?

图片附件: 游客没有浏览图片的权限,请 登录注册



[ 本帖最后由 TonyDeng 于 2015-2-21 03:06 编辑 ]

授人以渔,不授人以鱼。
2015-02-21 02:52
zwdbxlt
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2015-1-1
收藏
得分:0 
回复 5楼 TonyDeng
vs2013,我写的是c程序,不是cpp。在VS2013下通过了,不过shuzhuangfuzhi确实应该写成struct node *类型,我把它改过来问题依旧。
目前我通过struct node *p[]代替每次对malloc()的调用来分配内存地址,然后可以成功的运行了。
以下是我成功运行的程序:



#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;
}
2015-02-21 10:12
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
我貼圖的畫面就是vs2013,你那個語法上就錯的問題在被强制爲C編譯的情況下通過卻實際運行失敗,而在C++編譯模式下能檢查出來,你說還堅持C編譯有什麽意義?

授人以渔,不授人以鱼。
2015-02-21 10:18
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
我上面就說了,你這代碼標識符命名成這樣,根本很難閲讀,我本沒打算詳細調試,但把代碼黏貼到vs2013中,它立馬就報這個錯誤,連編譯都不用執行,而你自己修改後都說是這個問題造成的,可想而知這種把指針和整型數據混用的C許可有多大的隱患。是的,在C語言中,指針和整型的確可以混用,這是C面嚮底層的獨特思想,但在人類邏輯上,指針卻絕對不是整數,所以在C++語言中指出它是錯的,兩者處理方式的不同,結果在實踐上就檢驗到了。

在C++中,如果你真要把指針視爲整型數據,就要用强制類型轉換,這是C++語言用迫使程序員明確寫强制類型轉換的麻煩提示你是否真要這樣用,而它認爲,一旦你真這樣寫了,那麽就確實是你的真實意圖,而不是疏忽。在C語言中,卻隨便地認爲你這樣寫是經過思考的真實意圖。這就是兩種語言的優劣比較。我多次說過,越精通C的思想,就必然越排斥C++的思想,也多次提醒過那些以爲學C++之前必須學C還要學好的人注意,可惜聽得進去的不多。

授人以渔,不授人以鱼。
2015-02-21 10:35
zwdbxlt
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2015-1-1
收藏
得分:0 
回复 7楼 TonyDeng
您好,我运行成功的程序是在没有改变shuzhaungfuzhi的类型下运行成功的,即shuzhuangfuzhi仍然为整型,我强调c的意义是在c下我可以成功的运行一个程序,但在cpp根本编译都没通过。
我并没有说出现的问题是由于shuzhuangfuzhi为整型所致,我只是强调严格意义上应该shuangzhuangfuzhi改为指针型,事实是出现的问题并不是shuangzhuangfuzhi为int型所导致的。
2015-02-21 11:31
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
好吧,我閉嘴。

授人以渔,不授人以鱼。
2015-02-21 11:40
快速回复:诸神请看:第二次调用malloc给结构指针分配空间时出错 0xC0000374: 堆 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.052568 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved