va_arg 导致 std::bad_alloc
va_arg causes std::bad_alloc
每当我 运行 我的程序时,我都会抛出 std::bad_alloc
异常,这会导致中止发生。 std::bad_alloc
只有在调用 va_arg 时才会被抛出。奇怪的是崩溃的代码是由讲师提供的。我没有写崩溃的行。 Valgrind 告诉我这是由 new/new[]
引起的。为什么 va_arg 会造成这种情况? 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)
计划
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc Aborted
讲师设计了使用 va_args 的循环(我只是填写了它的作用)所以我不确定他的代码为什么会导致崩溃。我的代码会导致崩溃吗?有人可以提供一些见解吗?
"autism"
和 "Asperger's Syndrome"
是 const char *
值,而不是 string
值,因此尝试将它们读取为 string
s 会导致未定义的行为。
每当我 运行 我的程序时,我都会抛出 std::bad_alloc
异常,这会导致中止发生。 std::bad_alloc
只有在调用 va_arg 时才会被抛出。奇怪的是崩溃的代码是由讲师提供的。我没有写崩溃的行。 Valgrind 告诉我这是由 new/new[]
引起的。为什么 va_arg 会造成这种情况? 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)
计划
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc Aborted
讲师设计了使用 va_args 的循环(我只是填写了它的作用)所以我不确定他的代码为什么会导致崩溃。我的代码会导致崩溃吗?有人可以提供一些见解吗?
"autism"
和 "Asperger's Syndrome"
是 const char *
值,而不是 string
值,因此尝试将它们读取为 string
s 会导致未定义的行为。