簡體   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