std::bad_alloc连接两个向量时
std::bad_alloc when concatenating two vectors
连接两个向量时遇到一些问题。
std::vector<Transform3D> out;
for(double theta = 0; theta <= 2*M_PI ; theta+=1 )
{
for(double phi = 0; phi <= M_PI ; phi+=1 )
{
double sphere_x = obj_x + r*cos(theta)*sin(phi);
double sphere_y = obj_y + r*sin(theta)*sin(phi);
double sphere_z = obj_z + + r*cos(phi);
Transform3D<> transformation_matrix = transform(obj_x,obj_y,obj_z,sphere_x,sphere_y,sphere_z);
if(0.01<(transformation_matrix.P()[0] - current_x) ||
0.01<transformation_matrix.P()[1] - current_y ||
0.01<transformation_matrix.P()[2] - current_z)
{
cout << "Interpolate: " << endl;
std::vector<Transform3D> transformation_i = invKin_LargeDisplacement(transformation_matrix);
out.insert(out.end(),transformation_i.begin(),transformation_i.end());
}
else
{
cout << "OK" << endl;
out.push_back(transformation_matrix);
}
cout << out.size() << endl;
cout << sizeof(Transform3D<>) << endl;
}
}
out.insert(..)
似乎导致了bad_alloc
,但需要额外的数据。
在调试该问题时,我在运行for循环时打印了矢量的大小,并得到了以下输出:
Interpolate:
6346700
Interpolate:
12052200
Interpolate:
16476100
Interpolate:
20127501
Interpolate:
26474201
Interpolate:
32239601
Interpolate:
36748301
Interpolate:
40416502
Interpolate:
46763202
Interpolate:
52659402
Interpolate:
57349102
Interpolate:
61053903
Interpolate:
67400603
Interpolate:
73377503
Interpolate:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
Aborted (core dumped)
有没有什么方法可以避免得到bad_alloc
,同时仍然可以插值?
如果我们假设您的转换是一个具有4x4浮点数组(4字节)的PoD:
转换的大小=4*4*4=64字节
异常情况下的阵列大小=73377503
64*73377503=4696160192字节=4.696160192千兆字节
有了这么大的矢量,我想你的记忆力会耗尽。。。
您的机器中有多少内存?您真的需要4.7Gb的转换数据吗?
如果是这样的话,也许可以考虑压缩内存中的数据和/或将其写入文件缓存。
相关文章:
- 在C++中查找两个向量之间最相似的值
- 如何在C++中从两个向量生成所有可能的对?
- 返回两个向量 – 使用引用还是元组?
- 检查两个向量是否并行的最有效方法
- 如何获得比较两个向量对的子集
- 如何在 c++ 中对两个向量进行线性搜索?
- 如何在 c++ 中从两个向量创建 JSON 对象?
- C++如何同时删除位于两个向量中的 2 个指针?
- 如何通过 stl 容器和算法库计算两个向量的内积?
- 删除两个向量中的重复项
- 两个向量在犰狳中相等?
- 为什么我无法比较自定义类类型的两个向量?
- 通过块比较两个向量时,如何避免重复
- Boost.Test - 如何在交叉点上检查两个向量
- 通过移动从两个向量创建元组向量
- 如何在C 中合并两个向量
- R- armadillo c :用其他两个向量对向量进行排序
- 使用嵌套循环搜索两个向量并查看其属性
- 如何以精度换取速度来评估C++中两个向量的点积符号?(不特定于硬件)
- 我该如何XOR这两个向量的内容