2016-12-08 47 views
-2

每當我運行我的程序時,我都會得到一個std::bad_alloc異常,導致中止發生。只有在調用va_arg時纔會引發std::bad_alloc。奇怪的是,教師提供的崩潰代碼。我沒有寫下那條崩潰的路線。 Valgrind告訴我這是由new/new[]造成的。爲什麼va_arg造成這種情況? bad_alloc只在執行時發生(它也在其他地方執行)。va_arg會導致std :: bad_alloc

void Library::addKeywordsForItem(const Item* const item, int nKeywords, ...) 
{ 
    // the code in this function demonstrates how to handle a vararg in C++ 

    va_list   keywords; 
    string   keyword = "test"; 
    bool   successFlag = false; 
    sArray   keywordV; 
    cout << "Before lookupItem\n"; 
    Item*   item2 = lookupItem(item); 
    cout << "After lookupItem\n"; 

    va_start(keywords, nKeywords); 
    cout << "after va_start\n"; 
    for (int i = 0; i < nKeywords; i++) //this code adds the items to a map of set to create a fast access structure for keyword searches 
    { 
     cout << "before keyword assign\n"; 
     keyword = va_arg(keywords, string); //Crash here 
     cout << "after keyword assign\n"; 
     // do something with each keyword 
     cout << "before HERE\n"; 
     keywordV.push_back(keyword); //pushes keyword onto vector 
     cout << "HERE\n"; 
     successFlag = addToSMap(item, keyword, keywordDbase); //This function is literally a copy/paste of the code 
     //originally designed for this function 
    } 
    va_end(keywords); 

    //Sets in keywords 
    item2->setKeywords(keywordV); 

    if(!successFlag) //Should never execute 
     cout << "This code reeks verily of wrongness.\n"; 
} 

上面的代碼是從下面的代碼教練書面線稱爲

library->addKeywordsForItem(item, 2, "autism", "Asperger's Syndrome"); 

這裏有錯誤,我得到

Valgrind的

**5851** new/new[] failed and should throw an exception, but Valgrind 
**5851** cannot throw exceptions and so is aborting instead. Sorry. 
==5851== at 0x4C275AC: ??? (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) 
==5851== by 0x4C27DC6: operator new(unsigned long) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) 
==5851== by 0x4F57496: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_assign(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (basic_string.tcc:265) 
==5851== by 0x4F577E8: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) (basic_string.h:1181) 
==5851== by 0x4085F8: Library::addKeywordsForItem(Item const*, int, ...) (Library.cpp:79) 
==5851== by 0x401BB5: main (Asgmt04.cpp:38) 

計劃

終止扔「的std :: bad_alloc的」的一個實例後調用
什麼()的std :: bad_alloc的中止

教員設計,它使用va_args(我剛填充它做什麼的循環)所以我不確定他的代碼爲什麼導致崩潰。我的代碼是否導致這個崩潰?有人可以借鑑一些見解嗎?

+0

如果你有可變參數模板的C++ 11,爲什麼要使用'va_args'? –

+0

這是教練希望我們使用的內容。 –

+1

額外的參數是'addKeywordsForItem'實際的'字符串'變量,還是他們是「字符串」? – 1201ProgramAlarm

回答

4

"autism""Asperger's Syndrome"const char *值,而不是string價值,所以想讀他們爲string小號原因未定義的行爲。

+0

@Stargateur你會使用'const int'嗎,因爲知道10不能被修改是很重要的嗎? [原文如此] – immibis

+0

@Stargateur你被允許愛Rust,但C++不是Rust。 – immibis

+0

@Stargateur我想說,不同的語言有不同的約定。 C++中的約定不是把const放在一切可能的地方。 'std :: string const&'是很有用的,因爲它可以引用一個const std :: string'變量,一個非const的變量或一個臨時變量;這是一個有意義的區別,證明在那裏使用'const'是合理的。 – immibis

相關問題