2014-12-21 72 views
0

我寫了一個簡單的C++應用程序,它只讀取stdin中的行並將它們存儲在鏈表中的堆中。不知道爲什麼Valgrind濫用此代碼片段...那「無效的讀取」在哪裏?我錯了這種方式釋放分配給鏈接列表的內存?C++鏈接列表中的內存損壞

1_9.cpp

#include <iostream> 
#include <string> 

struct Node { 
    std::string str; 
    Node* next; 
}; 

int main() 
{ 
    std::string line; 
    Node* head = NULL; 
    Node* _curr = NULL; 
    Node* _new = NULL; 

    std::cout << "Ready for reading from stdin..." << std::endl; 
    while(std::getline(std::cin, line)) 
    { 
     if (!head) 
     { 
      head = new Node; 
      head->str = line; 
      _curr = head; 
     } 
     else 
     { 
      _new = new Node; 
      _new->str = line; 
      _curr->next = _new; 
      _curr = _new; 
     } 
    } 

    std::cout << "Reading finished. Here are the results:" << std::endl; 
    for (Node* _node = head; _node != NULL; _node = _node->next) 
    { 
     std::cout << "\t" << _node->str << std::endl; 
    } 

    std::cout << "Cleaning the memory..." << std::endl; 
    for (Node* _node = head; _node != NULL; _node = _node->next) 
    { 
     std::cout << "\t Deleting:" << _node->str << std::endl; 
     delete _node; 
    } 

    std::cout << "Bye!" << std::endl; 
    return 0; 
} 

1_9.test:

asdfhjkl 
ASFF 
12324124124 
opuoiupoiu 
1234 

Valgrind的輸出:

[[email protected] 01]$ valgrind --track-origins=yes --tool=memcheck ./1_9 < 1_9.test 
==4890== Memcheck, a memory error detector 
==4890== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al. 
==4890== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info 
==4890== Command: ./1_9 
==4890== 
Ready for reading from stdin... 
Reading finished. Here are the results: 
    asdfhjkl 
    ASFF 
    12324124124 
    opuoiupoiu 
    1234 
==4890== Conditional jump or move depends on uninitialised value(s) 
==4890== at 0x400E30: main (in /home/vitaly/prog/study/cpp/01/1_9) 
==4890== Uninitialised value was created by a heap allocation 
==4890== at 0x4C27965: operator new(unsigned long) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) 
==4890== by 0x400D63: main (in /home/vitaly/prog/study/cpp/01/1_9) 
==4890== 
Cleaning the memory... 
    Deleting: asdfhjkl 
==4890== Invalid read of size 8 
==4890== at 0x400EA0: main (in /home/vitaly/prog/study/cpp/01/1_9) 
==4890== Address 0x5a141d8 is 8 bytes inside a block of size 16 free'd 
==4890== at 0x4C28991: operator delete(void*) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) 
==4890== by 0x400E9B: main (in /home/vitaly/prog/study/cpp/01/1_9) 
==4890== 
    Deleting: ASFF 
    Deleting: 12324124124 
    Deleting: opuoiupoiu 
    Deleting: 1234 
Bye! 
==4890== 
==4890== HEAP SUMMARY: 
==4890==  in use at exit: 0 bytes in 0 blocks 
==4890== total heap usage: 30 allocs, 30 frees, 796 bytes allocated 
==4890== 
==4890== All heap blocks were freed -- no leaks are possible 
==4890== 
==4890== For counts of detected and suppressed errors, rerun with: -v 
==4890== ERROR SUMMARY: 6 errors from 2 contexts (suppressed: 2 from 2) 

UPD:增加了一些調試輸出。無法弄清楚爲什麼invalid read是在上一次循環的第一次迭代期間發出的?

+1

嘿,誰投票決定關閉這個問題後?這是完全有效的問題。 – Andrey

+0

@Andrey _「嘿誰投票結束這個問題?......」這是我(除其他外)。如果要求未定義的行爲構成一個關於SO的有效的,研究和調試的問題,或者應該通過以下方式關閉,這是有爭議的:1.無法從簡單/印刷錯誤中複製。 2.缺乏調試工作。 –

+0

@πάνταῥεῖ你投票的理由「必須包括理想的行爲,特定的問題或錯誤以及重現它所需的最短代碼」。它符合這個要求。我認爲這不是那裏最有趣的問題,但它是有效的。 – Andrey

回答

4

一個猜測是,您嘗試訪問已刪除的對象:

for_node = _node->nextdelete node;

+0

儘管在_node爲空的情況下,它在哪裏執行此操作?我看不到它。當然,循環的主體沒有被輸入。 –

+0

此問題與空無關。在被刪除後訪問_node是UB。 – drescherjm

+0

@Poldie第一次迭代表達式被執行,然後條件表達式。身體未達到。 http://en.cppreference.com/w/cpp/language/for – Andrey