简体   繁体   中英

std::bad_alloc when concatenating two vectors

I am having some issues when I concatenate 2 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(..) seems to cause the bad_alloc , but the extra data is needed.

While debugging the issue, I printed the size of the vector while running the for loop and got the following output:

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)

Is there some way I can avoid getting the bad_alloc , while still being able to interpolate?

If we assume your transform is a PoD with a 4x4 array of floats (4 bytes):

size of transform = 4*4*4 = 64 bytes
size of array at exception = 73377503

64 * 73377503 = 4696160192 Bytes = 4.696160192 Gigabytes

With a vector of this magnitude I expect you ran out of memory...

How much memory do you have in your machine and do you really need 4.7Gb of transform data?

If so perhaps consider either compressing the data in memory and/or writing it to a file cache.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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