2015-02-11 57 views
0

這是我工作的分配或不分配。讀取大小爲1的錯誤無效。 [凝望2小時]

code 

但是有問題的代碼。部分或全部可能是因爲未在節點(行135〜)中爲data分配空間,因爲linemain的迭代過程中正在更改。但是當我爲它分配空間時,在第135行,結果沒有太大變化,並且存在內存泄漏。我卡住了。

我即將發瘋。你能幫我解決錯誤嗎?

任何幫助表示讚賞。謝謝!

輸出:

[email protected]:~/os/hw1$ ./o f1.txt f2.txt o.txt 
*** Error in `./o': double free or corruption (fasttop): 0x0000000001e8b250 *** 
Aborted (core dumped) 

Valgrind的:

[email protected]:~/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) 
+1

'如果(newPtr->數據== NULL)免費(NEWPTR); '而且你在那之後立即引用'free()'d指針。釋放後您必須從該功能返回。 – 2015-02-11 18:24:30

+0

認真嗎? 'strtok(line,「\ n」);',只是'if(line [read-1] =='\ n')line [ - read] ='\ 0';'會很好,更明確。不要採取錯誤的方式,我認爲你將成爲一名優秀的程序員。 – 2015-02-11 18:28:09

+0

@iharob:我沒有通讀代碼來找到上下文,但你的第三點有不同的語義---它將值複製到先前指向的任何「ptr」中(而不是僅僅改變'ptr')。 – 2015-02-11 18:48:51

回答

2

後您免費line第一次你應該把它設置爲NULLlen0

if (line) 
    free(line); 
line = NULL; /* make it NULL so you can call getline() again or just don't free it yet */ 
len = 0; 

其他明智的後續調用getline()將嘗試訪問無效指針。

而且,我評論

if(newPtr->data == NULL) 
    free(newPtr); 

是錯誤的,因爲你會提領newPtr,所以你必須

if (newPtr->data == NULL) 
{ 
    free(newPtr); 
    return; 
} 
+0

良好的捕捉 - 並設置'len = 0'太。 – 2015-02-11 18:34:19

+0

@WeatherVane我不記得是否需要,讓我檢查手冊。 – 2015-02-11 18:35:30

+0

@WeatherVane它不會壞,但手冊說,如果第一個參數是「NULL」,那麼'* n'值將被忽略。 – 2015-02-11 18:36:24

1

線138,如果條件是假的(無法分配的字符串空間),那麼你流連忘返,即:

if(newPtr->data == NULL) { 
    free(newPtr); 
    return; 
} 

如果發生這種情況,肯定會導致記憶問題。可能有其他問題(仍在尋找...)

+0

嘿downvoter,這仍然是一個錯誤。 – 2015-02-11 18:30:07

+0

@WeatherVane因爲原來的代碼是在一個鏈接,我複製它格式化並粘貼在這裏。 – 2015-02-11 18:38:19