关于链表中内存的释放

thefutureisour 2012-07-31 03:32:07
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define ElemType int

typedef struct LNode
{
ElemType data;
LNode *next;
}*LinkList;


//初始化链表
void initialList(LinkList &l)
{
l = (LinkList)malloc(sizeof(LinkList));
if(!l)
exit(1);
l->next = NULL;
}

//删除链表
void deleteList(LinkList &l)
{
LinkList q;
while(l)
{
q = l->next;
free(l);
l = q;

}
}
在主函数中:
int main()
{
LinkList myList;
initialList(myList);
deleteList(myList);
return 0;
}
每次执行到deleteList就会出错,求教了。
...全文
164 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
thefutureisour 2012-07-31
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

C/C++ code

//初始化链表
void initialList(LinkList &l)
{
l = (LinkList)malloc(sizeof(LNode));
if(!l)
exit(1);
l->next = NULL;
}


改成sizeof(LNode)就可以了
[/Quote]不好意思,给错分了。。。
IVERS0N 2012-07-31
  • 打赏
  • 举报
回复
楼主你给错分了
cooljuly 2012-07-31
  • 打赏
  • 举报
回复
l = (LinkList)malloc(sizeof(LinkList));
你申请了一个指针那么大小的空间,却把它赋值给大小为8个字节的一个结构体指针。
在free的时候,要求free掉8个字节的结构体空间,这个地方在检查堆空间的参数时,会判断出错。
「已注销」 2012-07-31
  • 打赏
  • 举报
回复
#include "stackli.h"
#include "fatal.h"
#include <stdlib.h>

struct Node
{
ElementType Element;
PtrToNode Next;
};

/* START: fig3_40.txt */
int
IsEmpty( Stack S )
{
return S->Next == NULL;
}
/* END */

/* START: fig3_41.txt */
Stack
CreateStack( void )
{
Stack S;

S = malloc( sizeof( struct Node ) );
if( S == NULL )
FatalError( "Out of space!!!" );
S->Next = NULL;
MakeEmpty( S );
return S;
}

void
MakeEmpty( Stack S )
{
if( S == NULL )
Error( "Must use CreateStack first" );
else
while( !IsEmpty( S ) )
Pop( S );
}
/* END */

void
DisposeStack( Stack S )
{
MakeEmpty( S );
free( S );
}

/* START: fig3_42.txt */
void
Push( ElementType X, Stack S )
{
PtrToNode TmpCell;

TmpCell = malloc( sizeof( struct Node ) );
if( TmpCell == NULL )
FatalError( "Out of space!!!" );
else
{
TmpCell->Element = X;
TmpCell->Next = S->Next;
S->Next = TmpCell;
}
}
/* END */

/* START: fig3_43.txt */
ElementType
Top( Stack S )
{
if( !IsEmpty( S ) )
return S->Next->Element;
Error( "Empty stack" );
return 0; /* Return value used to avoid warning */
}
/* END */

/* START: fig3_44.txt */
void
Pop( Stack S )
{
PtrToNode FirstCell;

if( IsEmpty( S ) )
Error( "Empty stack" );
else
{
FirstCell = S->Next;
S->Next = S->Next->Next;
free( FirstCell );
}
}
/* END */
IVERS0N 2012-07-31
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

C/C++ code

//初始化链表
void initialList(LinkList &l)
{
l = (LinkList)malloc(sizeof(LNode));
if(!l)
exit(1);
l->next = NULL;
}


改成sizeof(LNode)就可以了
[/Quote]

原来是这样
「已注销」 2012-07-31
  • 打赏
  • 举报
回复
typedef int ElementType;
/* START: fig3_39.txt */
#ifndef _Stack_h
#define _Stack_h

struct Node;
typedef struct Node *PtrToNode;
typedef PtrToNode Stack;

int IsEmpty( Stack S );
Stack CreateStack( void );
void DisposeStack( Stack S );
void MakeEmpty( Stack S );
void Push( ElementType X, Stack S );
ElementType Top( Stack S );
void Pop( Stack S );

#endif /* _Stack_h */

/* END */
Corner 2012-07-31
  • 打赏
  • 举报
回复

//初始化链表
void initialList(LinkList &l)
{
l = (LinkList)malloc(sizeof(LNode));
if(!l)
exit(1);
l->next = NULL;
}

改成sizeof(LNode)就可以了
IVERS0N 2012-07-31
  • 打赏
  • 举报
回复
好奇怪的问题

结构体的2个成员交换下位置就可以运行成功
白雷 2012-07-31
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

不好意思,没注意看,以为是free(q)
[/Quote]

俺也经常犯这种错误……
Corner 2012-07-31
  • 打赏
  • 举报
回复
不好意思,没注意看,以为是free(q)
Corner 2012-07-31
  • 打赏
  • 举报
回复

void deleteList(LinkList &l)
{
LinkList q;
while(l)
{
q = l->next;
if(q)
free(l);
l = q;

}
}

q为0的时候就不要free了

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧