繁体   English   中英

级联两个向量时的std :: bad_alloc

[英]std::bad_alloc when concatenating two vectors

连接2个向量时出现一些问题。

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转换数据吗?

如果是这样,也许考虑压缩内存中的数据和/或将其写入文件缓存。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM