关于连续释放内存函数free()的疑问

saishow 2012-03-07 12:25:43
void freedel(Item * p)
{
while(p != NULL)
{
free(p);/*疑问,既然已经释放了p那p->netx也被释放了才对.按理来说p->netx应该是随机值,赋值给p岂不是会出错?*/
p = p->netx;
}
return;
}

程序不单步确实不出错.但是程序单步就会出错.
求解答.
...全文
260 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
东莞某某某 2012-03-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 kuyucman 的回复:]

释放的是变量p指向的内存,而p变量自身的值不变,指向next是有效的
[/Quote]
说错了!内存没了,该数据内成员是无效的
saishow 2012-03-07
  • 打赏
  • 举报
回复
刚刚翻阅了
C Primer Plus 第17章 高级数据表示17.2
程序中代码就是直接这样释放.

curren = head;
while(curren != null)
{
free(curren);
curren = curren->netx;
}

我将程序重新单步调试释放 也会出错.
猪头小哥 2012-03-07
  • 打赏
  • 举报
回复
+
[Quote=引用 2 楼 cfjtaishan 的回复:]


void freedel(Item * p)
{
Item *q;
while(p != NULL)
{
q = p ->next;
free(p);
p = q;
}
return;
}

我想你的代码是有问题的,你把指针p释放掉了,就不可以使……
[/Quote]
liubingqian 2012-03-07
  • 打赏
  • 举报
回复
单步调试和运行时的环境是不一样的。IDE不是模仿操作系统的。
letmegocc 2012-03-07
  • 打赏
  • 举报
回复
一般链表释放都要再定义一个中间指针变量,然后再释放的,这样你确定能释放????
“程序不单步确实不出错”,你确定所有的内存都释放了????
saishow 2012-03-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 kuyucman 的回复:]

释放的是变量p指向的内存,而p变量自身的值不变,指向next是有效的
[/Quote]
为何单步调试的时候也一同释放了?
liubingqian 2012-03-07
  • 打赏
  • 举报
回复
是释放链表吧?

void freedel(Item * p)
{
Item *t;
while(p != NULL)
{
t = p->next;
free(p);
p = t;
}
return;
}
自信男孩 2012-03-07
  • 打赏
  • 举报
回复

void freedel(Item * p)
{
Item *q;
while(p != NULL)
{
q = p ->next;
free(p);
p = q;
}
return;
}

我想你的代码是有问题的,你把指针p释放掉了,就不可以使用它了。再次使用结果是未定义的。我建议你不要用你的方法来释放。
东莞某某某 2012-03-07
  • 打赏
  • 举报
回复
释放的是变量p指向的内存,而p变量自身的值不变,指向next是有效的
kason2011 2012-03-07
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 kuyucman 的回复:]

释放的是变量p指向的内存,而p变量自身的值不变,指向next是有效的
[/Quote]void free ( void * ptr );
Notice that this function leaves the value of ptr unchanged, hence it still points to the same (now invalid) location, and not to the null pointer.
goldbeef 2012-03-07
  • 打赏
  • 举报
回复
[Quote=引用楼主 saishow 的回复:]
C/C++ code
void freedel(Item * p)
{
while(p != NULL)
{
free(p);/*疑问,既然已经释放了p那p->netx也被释放了才对.按理来说p->netx应该是随机值,赋值给p岂不是会出错?*/
p = p->netx;
}
return;
}

程序不单步确实不出错.但是程序单……
[/Quote]在free(p)之后,p原来指向的内存资源被回收,但是p的值还没有变,也就是说p指向的内存位置不变,但是由于p先前指向的位置的内存资源被系统回收,若在下次使用p之前,系统用了这些资源,那么p所指向
的位置的内容就可能发生变化,所以p->next有可能正确,有可能错误
ProgrammingRing 2012-03-07
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 saishow 的回复:]

刚刚翻阅了
C Primer Plus 第17章 高级数据表示17.2
程序中代码就是直接这样释放.
C/C++ code

curren = head;
while(curren != null)
{
free(curren);
curren = curren->netx;
}


我将程序重新单步调试释放 也会出错.
[/Quote]
C Primer Plus 那个例子有问题,那样释放了current就无法在引用了,得copy一份
赵4老师 2012-03-07
  • 打赏
  • 举报
回复
仅供参考
//将c:\\tmp文件夹下的所有文件的内容全部放到用malloc分配的内存中
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>
struct FB {
char fn[256];
size_t fl;
char *b;
struct FB *next;
struct FB *prev;
} *fh,*fb,*ft;
char ln[256];
char fpn[256];
FILE *af;
FILE *f;
int L,n;
int main() {
system("dir /b /a-d c:\\tmp\\*.* >c:\\allfn.txt");
af=fopen("c:\\allfn.txt","r");
if (NULL==af) {
printf("Can not open file c:\\allfn.txt!\n");
return 1;
}
fh=NULL;
fb=NULL;
n=0;
while (1) {
if (NULL==fgets(ln,256,af)) break;
L=strlen(ln);
if ('\n'==ln[L-1]) ln[L-1]=0;
printf("read %s\n",ln);
strcpy(fpn,"c:\\tmp\\");
strcat(fpn,ln);
ft=(struct FB *)malloc(sizeof(struct FB));
if (NULL==ft) {
printf("Can not malloc ft!\n");
fclose(af);
return 2;//之前的malloc在main退出后由操作系统自动free
}
printf("ft[%d]==%p\n",n,ft);
strcpy(ft->fn,fpn);
f=fopen(fpn,"rb");
if (NULL==f) {
printf("Can not open file %s!\n",fpn);
fclose(af);
return 3;//之前的malloc在main退出后由操作系统自动free
}
ft->fl=_filelength(fileno(f));
ft->b=malloc(ft->fl);
if (NULL==ft->b) {
printf("Can not malloc ft->b!\n");
fclose(f);
fclose(af);
return 4;//之前的malloc在main退出后由操作系统自动free
}
printf("ft[%d]->b==%p\n",n,ft->b);
if (ft->fl!=fread(ft->b,1,ft->fl,f)) {
printf("fread error!\n");
fclose(f);
fclose(af);
return 5;//之前的malloc在main退出后由操作系统自动free
}
fclose(f);
ft->next=NULL;

if (NULL==fh) {
ft->prev=NULL;
fh=ft;
} else {
fb->next=ft;
ft->prev=fb;
}
fb=ft;
n++;
}
fclose(af);
printf("-----list-----\n");
for (ft=fh;NULL!=ft;ft=ft->next) {
printf("%8d %s\n",ft->fl,ft->fn);
if (NULL!=ft) fb=ft;
}
printf("-----free-----\n");
n--;
if (NULL!=fh) {
for (ft=fb->prev;NULL!=ft;ft=ft->prev) {
if (NULL!=ft->next->b) {
printf("ft[%d]->b==%p\n",n,ft->next->b);
free(ft->next->b);
}
if (NULL!=ft->next) {
printf("ft[%d]==%p\n",n,ft->next);
free(ft->next);
}
n--;
}
if (NULL!=fh->b) {
printf("ft[0]->b==%p\n",fh->b);
free(fh->b);
}
printf("ft[0]==%p\n",fh);
free(fh);
}
return 0;
}
//C:\tmp\tmp\Debug>dir /a-d c:\tmp
// 驱动器 C 中的卷是 C_HD5_1
// 卷的序列号是 1817-D526
//
// c:\tmp 的目录
//
//找不到文件
//
//C:\tmp\tmp\Debug>tmp
//找不到文件
//-----list-----
//-----free-----
//
//C:\tmp\tmp\Debug>dir /a-d c:\tmp
// 驱动器 C 中的卷是 C_HD5_1
// 卷的序列号是 1817-D526
//
// c:\tmp 的目录
//
//2011-06-30 18:04 44,840 my_c.rar
//2011-06-30 17:18 1,036 err.frm
//2011-06-30 14:32 14,243 出租.txt
//2011-06-28 12:08 23,681 MSDN98书签.txt
// 4 个文件 83,800 字节
// 0 个目录 17,041,870,848 可用字节
//
//C:\tmp\tmp\Debug>tmp
//read my_c.rar
//ft[0]==00421800
//ft[0]->b==00520068
//read err.frm
//ft[1]==00421670
//ft[1]->b==0052AFC0
//read 出租.txt
//ft[2]==00421530
//ft[2]->b==00378F28
//read MSDN98书签.txt
//ft[3]==004213F0
//ft[3]->b==0052B3F8
//-----list-----
// 44840 c:\tmp\my_c.rar
// 1036 c:\tmp\err.frm
// 14243 c:\tmp\出租.txt
// 23681 c:\tmp\MSDN98书签.txt
//-----free-----
//ft[3]->b==0052B3F8
//ft[3]==004213F0
//ft[2]->b==00378F28
//ft[2]==00421530
//ft[1]->b==0052AFC0
//ft[1]==00421670
//ft[0]->b==00520068
//ft[0]==00421800
//
//C:\tmp\tmp\Debug>
liubingqian 2012-03-07
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 saishow 的回复:]

刚刚翻阅了
C Primer Plus 第17章 高级数据表示17.2
程序中代码就是直接这样释放.
C/C++ code

curren = head;
while(curren != null)
{
free(curren);
curren = curren->netx;
}


我将程序重新单步调试释放 也会出错.
[/Quote]
书上的这个写法显然是错误的。

69,371

社区成员

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

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