【发布时间】:2013-01-22 20:41:16
【问题描述】:
在尝试生成随机数向量时,我偶然发现了 std::bad_alloc 错误。这是我的代码:
#include "search.h"
#include "gtest/gtest.h"
int _size = 100;
std::vector<int> GetSortedVector(int size){
//init vector
std::vector<int> v(size);
//fill with random numbers
for (std::vector<int>::size_type i=0; i < v.size(); i++)
v.push_back( std::rand()%(2*size) );
//return the setup vector
return v;
}
//triggered automatically
TEST(BinarySearch, NonUniqueSorted){
std::vector<int> v = GetSortedVector(_size);//nothing moves farther than this line
}
P.S.:我现在确实使用generate(),但我仍然很好奇它为什么会失败。
【问题讨论】:
标签: c++ exception memory-management vector