【发布时间】:2013-08-17 14:47:50
【问题描述】:
因为 vector 得到 long unsigned int 调用 f(-1) 抛出 bad_alloc。我怀疑是用 2147483648 拨打电话,实际上是 18446744073709551615,因为它是 x64 系统。如何获取有关错误详细信息的信息?这可能是笼统的,我怎样才能得到比e.what()更多的细节?
void f(int i){
vector<int> v(i);
printf("vector size: %d", v.size());
}
int main(int argc, char** argv) {
//f(1); // vector size: 1
try{
f(-1); // terminate called after throwing an instance of 'std::bad_alloc'
//what(): std::bad_alloc
}catch(std::bad_alloc& e){
printf("tried to allocate: %d bytes in vector constructor", e.?);
}
return 0;
}
【问题讨论】:
-
您希望看到什么样的附加信息?
-
与您的问题无关,您调用的向量构造函数采用
size_t参数,而您传递的是int。提高警告级别,你的编译器会警告你这些事情。 -
是的,这是真的,我知道,这只是一个简短的测试,重要的是当通过 -1 时我会得到什么
-
运算符 new 通常使用 malloc() 实现,malloc 不提供任何额外信息。如果 malloc 由于操作系统调用失败而失败,您可以尝试查看 errno。
-
errno 只有 int 集,没有细节,不是吗?
标签: c++ exception memory vector stl