| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2966 人关注过本帖
标题:单向链表为什么会有内存错误。 还有凡是用malloc开辟的空间 一定要用free函 ...
只看楼主 加入收藏
waja
Rank: 2
等 级:论坛游民
帖 子:53
专家分:52
注 册:2012-7-27
结帖率:81.82%
收藏
已结贴  问题点数:10 回复次数:6 
单向链表为什么会有内存错误。 还有凡是用malloc开辟的空间 一定要用free函数释放吗?
#include <stdio.h>
#include <malloc.h>
#include <conio.h>
struct LNode
{
    int data;
    struct LNode *next;
};


struct LNode *create(int n)
{
    int i;
    struct LNode *head, *p1, *p2;
    int a;
    head = NULL;
    printf("Input the integers:\n");
    for(i = n; i > 0; i--)
    {
        p1 = (struct LNode*)malloc(sizeof(struct LNode));
        scanf("%d", &a);
        p1->data = a;
        if (head = NULL)
        {
            head = p1;
            p2 = p1;
        }
        else
        {
            p2->next = p1;
            p2  = p1;
        }

    }
    p2->next = NULL;
    return head;
}

void main()
{
    int n;
    struct LNode *q;
    printf("Inout the count of the nodes you want to create:\n");
    scanf("%d", &n);
    q = create(n);
    printf("the result is :\n");
    while(q)
    {
        printf("%d    ", q->data);
        q = q->next;
    }
    getch();
}

[ 本帖最后由 waja 于 2013-3-10 09:19 编辑 ]
搜索更多相关主题的帖子: free include create 
2013-03-10 09:18
zklhp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:china
等 级:贵宾
威 望:254
帖 子:11485
专家分:33241
注 册:2007-7-10
收藏
得分:5 
if (head = NULL)

对么 一般是不对的罢。。
2013-03-10 10:34
zklhp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:china
等 级:贵宾
威 望:254
帖 子:11485
专家分:33241
注 册:2007-7-10
收藏
得分:0 
至于那个malloc()和free()的么 规定就是这样的 堆栈内存由使用者负责释放 如果不释放 只要程序不退出 这块内存就占着

也存在一种情况 也是你学习的时候经常做的 一个程序 执行了一下就退出了 这个时候 malloc()分配的内存由操作系统负责释放 也就是操作系统负责擦屁股 至于擦的好不好 也是操作系统的事了。。

程序代码:
#include <stdio.h>
#include <malloc.h>
struct LNode
{
    int data;
    struct LNode *next;
};

struct LNode *create(int n)
{
    int i;
    struct LNode *head, *p1, *p2;
    int a;
    head = NULL;
    printf("Input the integers:\n");
    for(i = n; i > 0; i--)
    {
        p1 = (struct LNode*)malloc(sizeof(struct LNode));
        scanf("%d", &a);
        p1->data = a;
        if (NULL == head)
        {
            head = p1;
            p2 = p1;
        }
        else
        {
            p2->next = p1;
            p2  = p1;
        }
    }
    p2->next = NULL;
    return head;
}
int main(void)
{
    int n;
    struct LNode *q;
    printf("Inout the count of the nodes you want to create:\n");
    scanf("%d", &n);
    q = create(n);
    printf("the result is :\n");
    while(q)
    {
        printf("%d    ", q->data);
        q = q->next;
    }
    return 0;
}


代码简单改了一下 祝你学习顺利
2013-03-10 10:41
zklhp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:china
等 级:贵宾
威 望:254
帖 子:11485
专家分:33241
注 册:2007-7-10
收藏
得分:0 
以下是引用zklhp在2013-3-10 10:41:18的发言:

至于那个malloc()和free()的么 规定就是这样的 堆栈内存由使用者负责释放 如果不释放 只要程序不退出 这块内存就占着

也存在一种情况 也是你学习的时候经常做的 一个程序 执行了一下就退出了 这个时候 malloc()分配的内存由操作系统负责释放 也就是操作系统负责擦屁股 至于擦的好不好 也是操作系统的事了。。

 
#include  
#include  
struct LNode
{
    int data;
    struct LNode *next;
};
 
struct LNode *create(int n)
{
    int i;
    struct LNode *head, *p1, *p2;
    int a;
    head = NULL;
    printf("Input the integers:\n");
    for(i = n; i > 0; i--)
    {
        p1 = (struct LNode*)malloc(sizeof(struct LNode));
        scanf("%d", &a);
        p1->data = a;
        if (NULL == head)
        {
            head = p1;
            p2 = p1;
        }
        else
        {
            p2->next = p1;
            p2  = p1;
        }
    }
    p2->next = NULL;
    return head;
}
int main(void)
{
    int n;
    struct LNode *q;
    printf("Inout the count of the nodes you want to create:\n");
    scanf("%d", &n);
    q = create(n);
    printf("the result is :\n");
    while(q)
    {
        printf("%d    ", q->data);
        q = q->next;
    }
    return 0;
}


代码简单改了一下 祝你学习顺利
进一步讲 虽然学习的时候不释放问题不大 因为你的程序属于例子代码 sample code 很快就退出了 但实际的程序可能要运行很长时间的 这个时候 如果不释放内存可能会造成程序崩溃 就算不崩溃也会影响程序的资源占用和效率

所以 一定要释放 这是C语言内存管理的要求。。
2013-03-10 10:43
梦幻乐园
Rank: 2
等 级:论坛游民
帖 子:62
专家分:87
注 册:2012-10-25
收藏
得分:0 
用fclose也行的
2013-03-10 10:51
dengluoy
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:127
专家分:165
注 册:2013-2-5
收藏
得分:3 
你的链表算法有点小问题,还有if(head == NULL)而不是在if语言句中让heaf指向=NULL
你的最后地方,最好让p1 ->next= NULL ,同时在最后让p2指向p1 .  因为p2是起到追踪作用。
head指针是记忆链表首项的作用俗称(根指针) p1是用来实际操作。next是用来连接下一个结构。
看看对你有没有帮助。。我也是菜鸟。。

一同学习, 一同进步
2013-03-10 12:23
bcl01
Rank: 1
等 级:新手上路
帖 子:2
专家分:2
注 册:2011-8-1
收藏
得分:2 
malloc free 是一对的,必须有,
赋值才是 =
比较得用 ==
2013-03-10 12:40
快速回复:单向链表为什么会有内存错误。 还有凡是用malloc开辟的空间 一定要用 ...
数据加载中...
 
   



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

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