问题描述
这是code我工作
code
不过也有问题。部分或全部可能是因为中的节点数据
(第135行〜)未分配空间,因为行
正在改变在主迭代中
。但是,当我为它分配空间,在第135行,胜负并没有太大变化且有内存泄漏。我卡住了。
我快要发疯了。你能不能帮我修正错误?
任何帮助是AP preciated。谢谢!
输出:
A @ Ubuntu的:〜/ OS / HW1 $ ./o f1.txt f2.txt o.txt
***错误的`./o:双重释放或腐败(fasttop):0x0000000001e8b250 ***
中止(核心转储)
Valgrind的:
A @ Ubuntu的:〜/ OS / HW1 $的valgrind --leak检查=全./o f1.txt F2 .TXT o.txt
== == 21643 MEMCHECK,内存错误检测
== == 21643版权所有(C)2002至13年,和GNU GPL的,Julian Seward写等。
== == 21643 Valgrind的使用-3.10.1和LibVEX;与-h版权信息重新运行
== == 21643命令:./o f1.txt f2.txt o.txt
== == 21643
== == 21643大小为2写入无效
== == 21643在0x4C2F843:__GI_memcpy(vg_replace_strmem.c:917)
== == 21643通过0x4EA5ED7:getdelim(iogetdelim.c:115)
== == 21643通过0x400959:主(在/ home / A / OS / HW1 / O)
== == 21643地址0x51fc2c0是尺寸为120 free'd块内0字节
== == 21643在0x4C2BCD7:免费(vg_replace_malloc.c:473)
== == 21643通过0x4008F6:主(在/ home / A / OS / HW1 / O)
== == 21643
== == 21643大小为1的写入无效
== == 21643在0x4EA5FD4:getdelim(iogetdelim.c:122)
== == 21643通过0x400959:主(在/ home / A / OS / HW1 / O)
== == 21643地址0x51fc2c2是尺寸为120 free'd的块中2个字节
== == 21643在0x4C2BCD7:免费(vg_replace_malloc.c:473)
== == 21643通过0x4008F6:主(在/ home / A / OS / HW1 / O)
== == 21643
== == 21643大小为1的读无效
== == 21643在0x4EC28BC:strtok的(strtok.S:137)
== == 21643通过0x40092F:主(在/ home / A / OS / HW1 / O)
== == 21643地址0x51fc2c0是尺寸为120 free'd块内0字节
== == 21643在0x4C2BCD7:免费(vg_replace_malloc.c:473)
== == 21643通过0x4008F6:主(在/ home / A / OS / HW1 / O)
== == 21643
== == 21643大小为1的读无效
== == 21643在0x4EC28EC:strtok的(strtok.S:163)
== == 21643通过0x40092F:主(在/ home / A / OS / HW1 / O)
== == 21643地址0x51fc2c0是尺寸为120 free'd块内0字节
== == 21643在0x4C2BCD7:免费(vg_replace_malloc.c:473)
== == 21643通过0x4008F6:主(在/ home / A / OS / HW1 / O)
== == 21643
== == 21643大小为1的读无效
== == 21643在0x4EC28F3:strtok的(strtok.S:167)
== == 21643通过0x40092F:主(在/ home / A / OS / HW1 / O)
== == 21643地址0x51fc2c1是尺寸为120 free'd的块中1个字节
== == 21643在0x4C2BCD7:免费(vg_replace_malloc.c:473)
== == 21643通过0x4008F6:主(在/ home / A / OS / HW1 / O)
== == 21643
== == 21643大小为1的写入无效
== == 21643在0x4EC2919:strtok的(strtok.S:186)
== == 21643通过0x40092F:主(在/ home / A / OS / HW1 / O)
== == 21643地址0x51fc2c1是尺寸为120 free'd的块中1个字节
== == 21643在0x4C2BCD7:免费(vg_replace_malloc.c:473)
== == 21643通过0x4008F6:主(在/ home / A / OS / HW1 / O)
== == 21643
== == 21643大小为1的读无效
== == 21643在0x4C2EC31:STRCMP(vg_replace_strmem.c:755)
== == 21643通过0x400A67:插入(在/ home / A / OS / HW1 / O)
== == 21643通过0x400942:主(在/ home / A / OS / HW1 / O)
== == 21643地址0x51fc2c0是尺寸为120 free'd块内0字节
== == 21643在0x4C2BCD7:免费(vg_replace_malloc.c:473)
== == 21643通过0x4008F6:主(在/ home / A / OS / HW1 / O)
== == 21643
== == 21643大小为1的读无效
== == 21643在0x4C2DB62:strlen的(vg_replace_strmem.c:412)
== == 21643通过0x4EBF66D:的strdup(strdup.c:41)
== == 21643通过0x400ABB:插入(在/ home / A / OS / HW1 / O)
== == 21643通过0x400942:主(在/ home / A / OS / HW1 / O)
== == 21643地址0x51fc2c0是尺寸为120 free'd块内0字节
== == 21643在0x4C2BCD7:免费(vg_replace_malloc.c:473)
== == 21643通过0x4008F6:主(在/ home / A / OS / HW1 / O)
== == 21643
== == 21643大小为1的读无效
== == 21643在0x4C2DB74:strlen的(vg_replace_strmem.c:412)
== == 21643通过0x4EBF66D:的strdup(strdup.c:41)
== == 21643通过0x400ABB:插入(在/ home / A / OS / HW1 / O)
== == 21643通过0x400942:主(在/ home / A / OS / HW1 / O)
== == 21643地址0x51fc2c1是尺寸为120 free'd的块中1个字节
== == 21643在0x4C2BCD7:免费(vg_replace_malloc.c:473)
== == 21643通过0x4008F6:主(在/ home / A / OS / HW1 / O)
== == 21643
== == 21643大小为2读无效
== == 21643在0x4C2F840:__GI_memcpy(vg_replace_strmem.c:917)
== == 21643通过0x400ABB:插入(在/ home / A / OS / HW1 / O)
== == 21643通过0x400942:主(在/ home / A / OS / HW1 / O)
== == 21643地址0x51fc2c0是尺寸为120 free'd块内0字节
== == 21643在0x4C2BCD7:免费(vg_replace_malloc.c:473)
== == 21643通过0x4008F6:主(在/ home / A / OS / HW1 / O)
== == 21643
== == 21643大小为1的读无效
== == 21643在0x4C2EC48:STRCMP(vg_replace_strmem.c:755)
== == 21643通过0x400A67:插入(在/ home / A / OS / HW1 / O)
== == 21643通过0x400942:主(在/ home / A / OS / HW1 / O)
== == 21643地址0x51fc2c1是尺寸为120 free'd的块中1个字节
== == 21643在0x4C2BCD7:免费(vg_replace_malloc.c:473)
== == 21643通过0x4008F6:主(在/ home / A / OS / HW1 / O)
== == 21643
== == 21643免费无效()/删除/删除[] / realloc的()
== == 21643在0x4C2BCD7:免费(vg_replace_malloc.c:473)
== == 21643通过0x400985:主(在/ home / A / OS / HW1 / O)
== == 21643地址0x51fc2c0是尺寸为120 free'd块内0字节
== == 21643在0x4C2BCD7:免费(vg_replace_malloc.c:473)
== == 21643通过0x4008F6:主(在/ home / A / OS / HW1 / O)
== == 21643
== == 21643
== == 21643 HEAP摘要:
== == 21643在退出,使用:0块0字节
== == 21643总堆的使用情况:10 allocs,11的FreeS,1,878字节分配
== == 21643
== == 21643所有堆块被释放 - 无泄漏是可能的
== == 21643
== == 21643侦测到并SUP pressed错误计数,重新运行:-v
== == 21643错误摘要:从12上下文22错误(SUP pressed:0 0)
在你自由行
第一次你应该把它设置为 NULL
和 LEN
到 0
如果(行)
免费(线);
线= NULL; / *让它空,因此可以调用函数getline()再次或只是不释放它尚未* /
LEN = 0;
到函数getline其他明智的后续调用()
将尝试访问无效的指针。
另外,我评论
如果(newPtr->数据== NULL)
免费(NEWPTR);
是错误的,因为你会提领 NEWPTR
,无论如何,所以你必须
如果(newPtr->数据== NULL)
{
免费(NEWPTR);
返回;
}
This is the code I'm working on
code
However there are problems. Some or all might be because of not allocating space for data
in nodes (line 135~) because the line
is changing during iteration in main
. But when I allocate space for it, at line 135, the outcome doesn't change much and there is memory leak. I'm stuck.
I'm about to go crazy. Can you help me to fix errors?
Any help is appreciated. Thank you!
Output:
a@ubuntu:~/os/hw1$ ./o f1.txt f2.txt o.txt
*** Error in `./o': double free or corruption (fasttop): 0x0000000001e8b250 ***
Aborted (core dumped)
Valgrind:
a@ubuntu:~/os/hw1$ valgrind --leak-check=full ./o f1.txt f2.txt o.txt
==21643== Memcheck, a memory error detector
==21643== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==21643== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==21643== Command: ./o f1.txt f2.txt o.txt
==21643==
==21643== Invalid write of size 2
==21643== at 0x4C2F843: __GI_memcpy (vg_replace_strmem.c:917)
==21643== by 0x4EA5ED7: getdelim (iogetdelim.c:115)
==21643== by 0x400959: main (in /home/a/os/hw1/o)
==21643== Address 0x51fc2c0 is 0 bytes inside a block of size 120 free'd
==21643== at 0x4C2BCD7: free (vg_replace_malloc.c:473)
==21643== by 0x4008F6: main (in /home/a/os/hw1/o)
==21643==
==21643== Invalid write of size 1
==21643== at 0x4EA5FD4: getdelim (iogetdelim.c:122)
==21643== by 0x400959: main (in /home/a/os/hw1/o)
==21643== Address 0x51fc2c2 is 2 bytes inside a block of size 120 free'd
==21643== at 0x4C2BCD7: free (vg_replace_malloc.c:473)
==21643== by 0x4008F6: main (in /home/a/os/hw1/o)
==21643==
==21643== Invalid read of size 1
==21643== at 0x4EC28BC: strtok (strtok.S:137)
==21643== by 0x40092F: main (in /home/a/os/hw1/o)
==21643== Address 0x51fc2c0 is 0 bytes inside a block of size 120 free'd
==21643== at 0x4C2BCD7: free (vg_replace_malloc.c:473)
==21643== by 0x4008F6: main (in /home/a/os/hw1/o)
==21643==
==21643== Invalid read of size 1
==21643== at 0x4EC28EC: strtok (strtok.S:163)
==21643== by 0x40092F: main (in /home/a/os/hw1/o)
==21643== Address 0x51fc2c0 is 0 bytes inside a block of size 120 free'd
==21643== at 0x4C2BCD7: free (vg_replace_malloc.c:473)
==21643== by 0x4008F6: main (in /home/a/os/hw1/o)
==21643==
==21643== Invalid read of size 1
==21643== at 0x4EC28F3: strtok (strtok.S:167)
==21643== by 0x40092F: main (in /home/a/os/hw1/o)
==21643== Address 0x51fc2c1 is 1 bytes inside a block of size 120 free'd
==21643== at 0x4C2BCD7: free (vg_replace_malloc.c:473)
==21643== by 0x4008F6: main (in /home/a/os/hw1/o)
==21643==
==21643== Invalid write of size 1
==21643== at 0x4EC2919: strtok (strtok.S:186)
==21643== by 0x40092F: main (in /home/a/os/hw1/o)
==21643== Address 0x51fc2c1 is 1 bytes inside a block of size 120 free'd
==21643== at 0x4C2BCD7: free (vg_replace_malloc.c:473)
==21643== by 0x4008F6: main (in /home/a/os/hw1/o)
==21643==
==21643== Invalid read of size 1
==21643== at 0x4C2EC31: strcmp (vg_replace_strmem.c:755)
==21643== by 0x400A67: insert (in /home/a/os/hw1/o)
==21643== by 0x400942: main (in /home/a/os/hw1/o)
==21643== Address 0x51fc2c0 is 0 bytes inside a block of size 120 free'd
==21643== at 0x4C2BCD7: free (vg_replace_malloc.c:473)
==21643== by 0x4008F6: main (in /home/a/os/hw1/o)
==21643==
==21643== Invalid read of size 1
==21643== at 0x4C2DB62: strlen (vg_replace_strmem.c:412)
==21643== by 0x4EBF66D: strdup (strdup.c:41)
==21643== by 0x400ABB: insert (in /home/a/os/hw1/o)
==21643== by 0x400942: main (in /home/a/os/hw1/o)
==21643== Address 0x51fc2c0 is 0 bytes inside a block of size 120 free'd
==21643== at 0x4C2BCD7: free (vg_replace_malloc.c:473)
==21643== by 0x4008F6: main (in /home/a/os/hw1/o)
==21643==
==21643== Invalid read of size 1
==21643== at 0x4C2DB74: strlen (vg_replace_strmem.c:412)
==21643== by 0x4EBF66D: strdup (strdup.c:41)
==21643== by 0x400ABB: insert (in /home/a/os/hw1/o)
==21643== by 0x400942: main (in /home/a/os/hw1/o)
==21643== Address 0x51fc2c1 is 1 bytes inside a block of size 120 free'd
==21643== at 0x4C2BCD7: free (vg_replace_malloc.c:473)
==21643== by 0x4008F6: main (in /home/a/os/hw1/o)
==21643==
==21643== Invalid read of size 2
==21643== at 0x4C2F840: __GI_memcpy (vg_replace_strmem.c:917)
==21643== by 0x400ABB: insert (in /home/a/os/hw1/o)
==21643== by 0x400942: main (in /home/a/os/hw1/o)
==21643== Address 0x51fc2c0 is 0 bytes inside a block of size 120 free'd
==21643== at 0x4C2BCD7: free (vg_replace_malloc.c:473)
==21643== by 0x4008F6: main (in /home/a/os/hw1/o)
==21643==
==21643== Invalid read of size 1
==21643== at 0x4C2EC48: strcmp (vg_replace_strmem.c:755)
==21643== by 0x400A67: insert (in /home/a/os/hw1/o)
==21643== by 0x400942: main (in /home/a/os/hw1/o)
==21643== Address 0x51fc2c1 is 1 bytes inside a block of size 120 free'd
==21643== at 0x4C2BCD7: free (vg_replace_malloc.c:473)
==21643== by 0x4008F6: main (in /home/a/os/hw1/o)
==21643==
==21643== Invalid free() / delete / delete[] / realloc()
==21643== at 0x4C2BCD7: free (vg_replace_malloc.c:473)
==21643== by 0x400985: main (in /home/a/os/hw1/o)
==21643== Address 0x51fc2c0 is 0 bytes inside a block of size 120 free'd
==21643== at 0x4C2BCD7: free (vg_replace_malloc.c:473)
==21643== by 0x4008F6: main (in /home/a/os/hw1/o)
==21643==
==21643==
==21643== HEAP SUMMARY:
==21643== in use at exit: 0 bytes in 0 blocks
==21643== total heap usage: 10 allocs, 11 frees, 1,878 bytes allocated
==21643==
==21643== All heap blocks were freed -- no leaks are possible
==21643==
==21643== For counts of detected and suppressed errors, rerun with: -v
==21643== ERROR SUMMARY: 22 errors from 12 contexts (suppressed: 0 from 0)
After you free line
the first time you should set it to NULL
and len
to 0
if (line)
free(line);
line = NULL; /* make it NULL so you can call getline() again or just don't free it yet */
len = 0;
other wise subsequent calls to getline()
will try to access the invalid pointer.
Also, as I commented
if(newPtr->data == NULL)
free(newPtr);
is wrong because you will dereference newPtr
anyway, so you must
if (newPtr->data == NULL)
{
free(newPtr);
return;
}
这篇关于分配或不分配。大小为1的错误无效读取。 [凝望2小时]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!