螺栓轴力检测的必要性:关于C语言的free()函数

来源:百度文库 编辑:神马品牌网 时间:2024/05/02 09:51:20
#include <stdio.h>
#include <stdlib.h>
#define LEN sizeof(struct LNode)

int num = 0;

struct LNode
{
int num;
struct LNode* next;
};

struct LNode* talloc(void)
{
return (struct LNode*)malloc(LEN);
}

void dateget(struct LNode *_root)
{
_root->num = num++;
_root->next = NULL;
}

struct LNode *addroot(struct LNode *_root)
{
struct LNode *p = NULL, *p2 = NULL;

if(_root == NULL)
{
_root = talloc();
dateget(_root);
}
else
{
for(p=_root; p->next != NULL; p=p->next)
;
p2 = talloc();
dateget(p2);
p->next = p2;
}
return _root;
}

struct LNode *kill(struct LNode *_root, int num)
{
struct LNode *p=_root, *p2=NULL;

if(p->num == num)
{
_root = _root->next;
free(p); //释放第一个结点
}
else
{
for(;p->next->num != num && p->next != NULL; p=p->next)
;
if(p->next->num == num)
{

}
}
return _root;
}

main()
{
struct LNode *_root = NULL;
char i;

for(i=0; i < 10; i++)
{
_root = addroot(_root);
}

/*_root = */kill(_root,0); //我释放了第一个结点,怎么还能正常输出

for(; _root->next != NULL; _root=_root->next)
{
printf("_root=%x next=%x num=%d\n",_root, _root->next, _root->num);
}
printf("_root=%x next=%x num=%d\n",_root, _root->next, _root->num);
}

这是我还没写好的一个简单的链表,写到一半突然发现不会用free函数拉!
请教free函数是怎么经行释放的,或者是我那写错了

struct LNode *addroot(struct LNode *_root)
{
struct LNode *p = NULL, *p2 = NULL;

if(_root == NULL)
{
_root = talloc();
dateget(_root);
}
else
{
for(p=_root; p->next != NULL; p=p->next)
;
p2 = talloc();
dateget(p2);
p->next = p2;
}
return _root;
}
======================================
???? for?
for(p=_root; p->next != NULL; p=p->next)
;

是啊,那个分号是怎么回事?