| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 877 人关注过本帖, 1 人收藏
标题:链表分配内存失败,求原因……
只看楼主 加入收藏
thlgood
Rank: 5Rank: 5
等 级:职业侠客
帖 子:281
专家分:381
注 册:2010-9-24
结帖率:91.43%
收藏(1)
已结贴  问题点数:20 回复次数:10 
链表分配内存失败,求原因……
程序代码:
#include <stdio.h>
#include <stdlib.h>
#define MallocMemory (Nape *)malloc(sizeof(Nape))

typedef struct Nape Nape;

struct Nape                          //定义多项式链表
{
    int Factor;                      //系数
    int Index;                       //指数
    struct Nape *next;
};

Nape *IniNape(Nape *h);              //输入函数链表
void ShowNape(Nape *h);              //输出函数链表
Nape *AddNape(Nape *h1, Nape *h2);   //多项式相加

int main()
{
    /*定义3个函数 Func是Function(函数)的缩写*/
    Nape *Func1 = NULL, *Func2 = NULL, *Func3 = NULL;
    Func1 = IniNape(Func1);
    Func2 = IniNape(Func2);
    getchar();

    Func3 = AddNape(Func1, Func2);   //多项式相加
    ShowNape(Func3);

    return 0;
}

//*********定义输入链表函数*********
Nape *IniNape(Nape *h)
{
    Nape *temp1, *temp2;
    temp1 = temp2 = MallocMemory;
    printf("请输入系数:");
    scanf("%d", &temp2->Factor);
    printf("请输入指数:");
    scanf("%d", &temp2->Index);
    temp2->next = NULL;
    while (temp2->Factor != 100)
    {
        if (h == NULL)
        {
            h = temp2;
        }
        else
        {
            temp1->next = temp2;
        }
        temp1 = temp2;
        temp2 = MallocMemory;
        if (temp2 != NULL)
        {
            int i = 0;
            printf("请输入系数(若输入100),结束输入:");
            scanf("%d", &i);
            if(i == 100)
            {
                temp2 = NULL;
                break;
            }
            else
            {
                temp2->Factor = i;
                printf("请输入指数:");
                scanf("%d", &temp2->Index);
                temp2->next = NULL;
            }
        }
    }
    fflush(stdin);
    printf("\n\n输入完毕!你输入的多项式为:\n\n");
    ShowNape(h);
    printf("\n\n");

    return h;
}

//***********输出链表函数*************
void ShowNape(Nape *h)
{
    Nape *temp = h;
    printf("%d(X)^%d", temp->Factor, temp->Index);
    temp = temp->next;
    while (temp != NULL)
    {
        if(temp->Factor > 0)
        {
            putchar('+');
        }
        printf("%d(X)^%d", temp->Factor, temp->Index);
        temp = temp->next;
    }
}

//*************多项式相加*************
Nape *AddNape(Nape *h1, Nape *h2)
{
    int flag = 0;
    Nape *temp1 = h1, *temp2 = h2, *head = NULL, *temp3;
    system("cls");
    printf("\n\n\n多项式相加\n\n\n");

    /*先把h1链表完整的复制到head中*/
    head = temp3 = MallocMemory;
    while (1)
    {
        temp3->Factor = temp1->Factor;
        temp3->Index = temp1->Index;
        if(temp1->next != NULL)
        {
            temp3->next = MallocMemory;
            temp3 = temp3->next;
            temp1 = temp1->next;
        }
        else
        {
            temp3->next = NULL;
            break;
        }
    }

    while (temp2 != NULL)
    {
        flag = 0;
        temp3 = head;
        while (temp3 != NULL)
        {
            if (temp3->Index == temp2->Index)
            {
                temp3->Factor += temp2->Factor;
                temp3 = temp3->next;
                temp2 = temp2->next;
                flag = 1;
                break;
            }
            else
            {
                temp3 = temp3->next;
            }
        }//while (temp3 != NULL)结束

        //如果没有找到,则复制到表尾
        if (flag == 0)
        {
            if((temp3 = MallocMemory) == NULL);
            {
                printf("内存分配失败!\n\n");
                getchar();
                exit(0);
            }
            temp3->Factor = temp2->Factor;
            temp3->Index = temp2->Index;

            temp3->next = NULL;
            temp2 = temp2->next;
        }
    }//while (temp2 != NULL) 结束*/

    return head;
}
==========================================================
此程序是把两个一元多项式相加起来,假设我们输入第一元多项式为 2x^3+4x^5,第二个元多项式为 3x^4,那么在运行的时候会出项:“分配内存失败”的现象,请问为什么会“分配内存失败”,怎么修改?
搜索更多相关主题的帖子: 内存 
2011-04-19 13:02
thlgood
Rank: 5Rank: 5
等 级:职业侠客
帖 子:281
专家分:381
注 册:2010-9-24
收藏
得分:0 
用一句话来描述上面的问题就是:

先创建一个链表,创建完了之后,就使用这个链表,在使用的时候偶尔会往链表后面加一个节点,可是就在加节点的时候提示:“内存分配失败……”

o(∩∩)Linux & Python 群:187367181
2011-04-19 13:06
njkido
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:224
专家分:1184
注 册:2011-3-8
收藏
得分:0 
if((temp3 = MallocMemory) == NULL);

[ 本帖最后由 njkido 于 2011-4-19 13:36 编辑 ]
2011-04-19 13:32
thlgood
Rank: 5Rank: 5
等 级:职业侠客
帖 子:281
专家分:381
注 册:2010-9-24
收藏
得分:0 
回复 3楼 njkido
这个程序是我自己写的。

我是想问:为什么会分配内存失败,而不是问为什么会输出:“分配内存失败”

o(∩∩)Linux & Python 群:187367181
2011-04-19 13:56
njkido
Rank: 9Rank: 9Rank: 9
等 级:蜘蛛侠
帖 子:224
专家分:1184
注 册:2011-3-8
收藏
得分:0 

那句话末尾多了个分号 会打印“分配内存失败” 其实temp3不是NULL
不过有分配失败的隐患 malloc没有free 内存泄漏了呗
2011-04-19 14:04
dewolf
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-4-13
收藏
得分:0 
以下是引用njkido在2011-4-19 13:32:19的发言:

if((temp3 = MallocMemory) == NULL);


结尾多了一个“;”,造成下面的语句始终都会执行。
2011-04-19 14:14
thlgood
Rank: 5Rank: 5
等 级:职业侠客
帖 子:281
专家分:381
注 册:2010-9-24
收藏
得分:0 
回复 6楼 dewolf
去了分号还是有问题,链表貌似断了,后面加上去的节点没有输出……

o(∩∩)Linux & Python 群:187367181
2011-04-19 14:18
thlgood
Rank: 5Rank: 5
等 级:职业侠客
帖 子:281
专家分:381
注 册:2010-9-24
收藏
得分:0 
我把节点加到链表头试试~

o(∩∩)Linux & Python 群:187367181
2011-04-19 17:52
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
收藏
得分:0 
不是链表节点内存分配失败  是你的指针乱了  在保护模式下4GB的内存空间足够你分配的

好好理一下

                                         
===========深入<----------------->浅出============
2011-04-19 19:31
thlgood
Rank: 5Rank: 5
等 级:职业侠客
帖 子:281
专家分:381
注 册:2010-9-24
收藏
得分:0 
以下是引用laoyang103在2011-4-19 19:31:51的发言:

不是链表节点内存分配失败  是你的指针乱了  在保护模式下4GB的内存空间足够你分配的

好好理一下


那要怎么改啊?指针和链表可是我的软肋啊
我都怕了,已经折腾很久了

o(∩∩)Linux & Python 群:187367181
2011-04-19 21:10
快速回复:链表分配内存失败,求原因……
数据加载中...
 
   



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

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