对使用gdb查看矢量值感到困惑

时间:2015-03-11 02:29:59

标签: c++ debugging gdb

我有一个函数读取外部值并在其中存储一些变量。头文件如下:

struct surface {
    int NPoints;
    int NTriangles;
    vector <double> coor_x;
    vector <double> coor_y;
    vector <double> coor_z;
    vector <double> coor_nx;
    vector <double> coor_ny;
    vector <double> coor_nz;
    vector <int> indexa;
    vector <int> indexb;
    vector <vector <int> > v2t;
};

class Solve{
 public:
     ErrorCode loadRawn(char * fname, PBRawnAndQuadInfo * surf);

}

我遇到的问题如下:

ErrorCode Solve::load(char * fname, Info * surface) {

  ifstream fin(fname);
  fin >> surface->NPoints >> surface->NTriangles;

  for (int i=0; i<surface->NPoints; i++) {
    double xx,yy,zz;
    fin >> xx >> yy >> zz;
    surface->coor_x.push_back(xx);
    fin >> xx >> yy >> zz;
    surface->coor_nx.push_back(xx);
  }

  surface->v2t.resize(surface->NPoints);

ofstream file;
file.open("output.txt");

  for (int i=0; i<surf->NTriangles; i++) {
    int a,b,c;
    fin >> a >> b >> c;
    surface->indexa.push_back(a);
    surface->indexb.push_back(b);
    surface->v2t[a].push_back(i);
    surface->v2t[b].push_back(i);
    file<<"indices: "<<surface->indexa[i]<<" "<<surface->indexb[i]<<endl;
    file<<"a: "<<a<<" b "<<b<<" c "<<c<<" i "<<i;
    file<<"v2t: "<<surface->vt2[ surface->indexa[i] ][i]<<" "<<surface->v2t[ surface->indexbi] ][i]<<endl;MolEnergy
  }
  file.close();
  return 0;
}

我在fname函数中用于load的输入文件中的实际值是:

24 44
27.566549 3.457037 62.057549 -0.221094 -0.700101 -0.222706
27.566549 4.397549 61.117039 -0.221094 -0.221094 -0.701714
[get rows of 6 columns of values until 25th line]
...
0 2 1 //this is 26th line
0 3 4
1 3 0
0 2 1
0 3 4
1 3 0
3 5 4
1 6 7
2 6 1
3 1 8
1 7 8
8 5 3
9 5 8
7 6 10
7 11 8
10 11 7
8 11 9
13 2 0
12 2 13
4 14 0
14 13 0
15 4 5
14 4 15
2 12 6
6 12 16
5 9 15
9 17 15
10 16 18
6 16 10
11 10 19
19 10 18
9 19 17
11 19 9
13 20 12
14 20 13
21 20 14
15 21 14
12 22 16
20 22 12
21 23 20
20 23 22
17 21 15
23 21 17
16 22 18
23 18 22
19 18 23
17 19 23

`

然而,当我使用gdb在a行打印bfin >> a >> b的值时,a的初始值为0,b的初始值为24。在我打印surface->indexa[i].push_back(a)a时的b行,值分别为0和10。直到surf->vert2tris[a].push_back(i);行,ab的打印值分别在0和2处才正确。 gdb应该这样做吗?

我更难理解的是v2t变量发生了什么。在output.txt文件中,它显示v2t的前几个值为0 01 0,然后是34667504 -738656440。这绝对是错误的,因为程序不应该这样做。当我输入 print surface->v2t

我收到以下长信息 `$ 16 = {&gt ;, std :: allocator&gt; &GT; &GT;&GT; = {     _M_impl = {&gt; &GT;&GT; = {&lt; __ gnu_cxx :: new_allocator&gt; &GT;&GT; = {},},_ M_start = 0x1c53c70,_M_finish = 0x1c53eb0,       _M_end_of_storage = 0x1c53eb0}},}

发生了什么事?如何检查导致output.txt文件中打印出的v2t值明显不正确的原因?

编辑:我添加了下面的行来检查越界问题,但这些行表明越界不是问题,因为“跳过”从未在输出文件中显示

file<<"surface->v2t.size()"<<surface->v2t.size()<<endl;
if (a >= surface->v2t.size() || b >= surface->v2t.size() )
{file<<"skip:"<<endl;}

0 个答案:

没有答案