2016-03-21 199 views
0

我試圖用下面的一段代碼來解決hackerrank的even tree task來讀取輸入(std::cin替換爲自定義字符串數據以便有輸入和程序在這裏一處代碼):std :: vector <std :: vector <int>> push_back給出堆緩衝區溢出

#include <iostream> 
#include <vector> 
#include <sstream> 

int main() 
{ 
    std::istringstream input("10 9\n2 1\n3 1\n4 3\n5 2\n6 1\n7 2\n8 6\n9 8\n10 8\n"); 
    std::cin.rdbuf(input.rdbuf()); 

    int n,m; 
    std::cin >> n >> m; 

    std::vector<std::vector<int>> v(n); 

    //std::vector<std::vector<int>> v(n, std::vector<int>(n, -1)); 

    int ui, vi; 
    while (m--) 
    { 
    std::cin >> ui >> vi; 
    v[ui].push_back(vi); 
    v[vi].push_back(ui); 
    } 
} 

第二個數字將是邊緣的(後續的數對的數目),所以我可以預測有多少元件在載體中,我將需要。

此代碼給我下面消毒錯誤(同樣的錯誤與註釋行):

clang++-3.6 -g -Wall -fsanitize=address --std=c++11 main.cpp && ./a.out 
================================================================= 
==11606==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x611000009ff8 at pc 0x0000004e0beb bp 0x7ffd09cb9ab0 sp 0x7ffd09cb9aa8 
READ of size 8 at 0x611000009ff8 thread T0 
    #0 0x4e0bea (PATH/a.out+0x4e0bea) 
    #1 0x4dfa28 (PATH/a.out+0x4dfa28) 
    #2 0x7f407bd75ec4 (/lib/x86_64-linux-gnu/libc.so.6+0x21ec4) 
    #3 0x438227 (PATH/a.out+0x438227) 

0x611000009ff8 is located 8 bytes to the right of 240-byte region [0x611000009f00,0x611000009ff0) 
allocated by thread T0 here: 
    #0 0x4de672 (PATH/a.out+0x4de672) 
    #1 0x4ecf8a (PATH/a.out+0x4ecf8a) 
    #2 0x4eccd5 (PATH/a.out+0x4eccd5) 
    #3 0x4eca90 (PATH/a.out+0x4eca90) 
    #4 0x4ec70f (PATH/a.out+0x4ec70f) 
    #5 0x4ea89a (PATH/a.out+0x4ea89a) 
    #6 0x4e047a (PATH/a.out+0x4e047a) 
    #7 0x4df8f2 (PATH/a.out+0x4df8f2) 
    #8 0x7f407bd75ec4 (/lib/x86_64-linux-gnu/libc.so.6+0x21ec4) 

Shadow bytes around the buggy address: 
    0x0c227fff93a0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 
    0x0c227fff93b0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 
    0x0c227fff93c0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 
    0x0c227fff93d0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 
    0x0c227fff93e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
=>0x0c227fff93f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 fa[fa] 
    0x0c227fff9400: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 
    0x0c227fff9410: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 
    0x0c227fff9420: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 
    0x0c227fff9430: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 
    0x0c227fff9440: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 
Shadow byte legend (one shadow byte represents 8 application bytes): 
    Addressable:   00 
    Partially addressable: 01 02 03 04 05 06 07 
    Heap left redzone:  fa 
    Heap right redzone:  fb 
    Freed heap region:  fd 
    Stack left redzone:  f1 
    Stack mid redzone:  f2 
    Stack right redzone:  f3 
    Stack partial redzone: f4 
    Stack after return:  f5 
    Stack use after scope: f8 
    Global redzone:   f9 
    Global init order:  f6 
    Poisoned by user:  f7 
    Container overflow:  fc 
    Array cookie:   ac 
    Intra object redzone: bb 
    ASan internal:   fe 
    Left alloca redzone:  ca 
    Right alloca redzone: cb 
==11606==ABORTING 

缺少什麼我在這裏?

編輯

好了,所以我已經找到了解決方案,將是emplace_back默認std::vector<int>v之一:

std::vector<std::vector<int>> v(n); 
for (int i = 0; i < n; ++i) v.emplace_back(); 

但爲什麼不前,因爲構造函數size_type工作cppreference

3)構造包含呃計數默認插入的T實例。沒有複製。

+0

'n = 10',你在讀'10 8行時訪問'v [10]'(超出範圍)。我沒有閱讀你想要解決的任務,但是這聽起來像是一個「一個一個」的錯誤。你是不是指'v [ui-1]'和'v [vi-1]'? – leemes

+0

您可以使用-D_GLIBCXX_DEBUG嘗試g ++編譯器,它將使用帶有範圍檢查的安全容器。 – Radek

回答

3

在這一行

std::vector<std::vector<int>> v(n); 

創建矢量具有10個元素,這意味着可以用索引[0,9]包容訪問元素。在最後的數據中,你有10 8,這將導致超出範圍的訪問。如果你的數據是在[1,10]範圍需要調整指數:

v[ui-1].push_back(vi); 
v[vi-1].push_back(ui); 

PS,因爲你創建std::vector有10個元素你除了避免錯誤,然後你在循環,這使得有效增加10元以上索引[0,19]。您可以通過修復代碼:

std::vector<std::vector<int>> v(n+1); 

無需額外的循環,如果你想使用在[1,10]區間索引(元素與索引0會依然存在,雖然存在)。

您可以考慮使用std::map<std::vector<int>>,你不必擔心指標:

#include <iostream> 
#include <vector> 
#include <map> 
#include <sstream> 

int main() 
{ 
    std::istringstream input("10 9\n2 1\n3 1\n4 3\n5 2\n6 1\n7 2\n8 6\n9 8\n10 8\n"); 
    std::cin.rdbuf(input.rdbuf()); 

    int n,m; 
    std::cin >> n >> m; 

    std::map<std::vector<int>> v; 

    int ui, vi; 
    while (m--) 
    { 
    std::cin >> ui >> vi; 
    v[ui].push_back(vi); 
    v[vi].push_back(ui); 
    } 
} 
在這種情況下,您只能使用索引數據,您使用

,但訪問元素的索引將是顯著慢。如果您不關心數據是否在容器內進行排序,您也可以考慮std::unordered_map以加快訪問速度。

相關問題