如何在动态二维数组中存储数据,在结构体中声明,该结构体也被c++中的另一个结构体使用

How to store data in a dynamic 2D array, declared inside a struct which is also used by another struct in C++

本文关键字:结构体 c++ 另一个 声明 二维数组 动态 存储 数据      更新时间:2023-10-16

我正试图调试别人的代码,我不能从文件中读取数据到一个变量,在头文件中声明。我从。c和。h文件附加的片段。

//c

#include <cstdio>
#include <cstdlib> 
#include <cmath>
#include <fstream>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include "test.H"
int main()
{
 Initialize();
 return(0);
}

//test.C。

#include <fstream>
#include <utility>
#include <iostream>
#include <string>
#include <cstring>
#include "test.H"
#include "BB.H"
using namespace std;
#define file_to_be_read "file_to_be_read"
int NUM[10] = {3,2,3,4,5,2,3,1,3,4};
OBJECTS * OBJECTS::m_list[10];
void OBJECTS::createObjects()
{
 //cout << "Print somethin'" << endl;
 OBJECTS * objs = new OBJECTS(A_size, NUM[A]);
 m_list[A] = objs;
 objs = new OBJECTS(B_size, NUM[B]);
 m_list[B] = objs;
 objs = new OBJECTS(C_size, NUM[C]);
 m_list[C] = objs;     
 objs = new OBJECTS(D_size, NUM[D]);
 m_list[D] = objs;
 objs = new OBJECTS(E_size, NUM[E]);
 m_list[E] = objs;     
 objs = new OBJECTS(F_size, NUM[F]);
 m_list[F] = objs;
 objs = new OBJECTS(G_size, NUM[G]);
 m_list[G] = objs;     
 objs = new OBJECTS(H_size, NUM[H]);
 m_list[H] = objs;
 objs = new OBJECTS(I_size, NUM[I]);
 m_list[I] = objs;     
 objs = new OBJECTS(J_size, NUM[J]);
 m_list[J] = objs;
 char name[100];
 sprintf(name,file_to_be_read);
 ifstream fin(name);
 // Load the file.
 string buf;
 for (int i = 0; i < 10; i++)
 {
  objs = m_list[i];
  for (int j = 0; j < NUM[i]; j++)
  {
   fin >> buf; // read separator line.
   cout << buf << endl;
   for (int k = 0; k < objs->size_m; k++)
   {
    fin >> buf;
    cout << buf << endl;
    fin >> objs->sub_objs[j].coords[k][0];  // Segmentation error 
    fin >> objs->sub_objs[j].coords[k][1];
   }
  }
 }
}
void Initialize() 
{
 OBJECTS::createObjects();
}

test.H文件
#ifndef _test_H
#define _test_H
#include <cmath>
#include <cassert>
#include <vector>
#define A 0
#define B 1
#define C 2
#define D 3 
#define E 4
#define F 5
#define G 6
#define H 7 
#define I 8 
#define J 9
extern int NUM[];

//////////////////////
struct SUBOBJECTS
{
 SUBOBJECTS(int size)
 {
  coords = new double[size][2];
 }
 double (*coords)[2];
};
//////////////////////
struct OBJECTS 
{
 OBJECTS(int size, int number) : 
 size_m(size), num_m(number)
 {
  std::vector<SUBOBJECTS> sub_objs(number,size);
 }
 static void createObjects();
 static OBJECTS * m_list[10];
 SUBOBJECTS * sub_objs;
 int size_m;
 int num_m;
};
void Initialize();
#endif
//BB.H

 const int A_size = 2;
 const int A_size = 2;
 const int B_size = 3;
 const int C_size = 3;
 const int D_size = 2;
 const int E_size = 2;
 const int F_size = 1;
 const int G_size = 1;
 const int H_size = 3;
 const int I_size = 3;
 const int J_size = 2;

file_to_be_read

#######################  
0.658 1.291  
1.891 1.753  
#----------------------  
2.570 2.955  
3.747 3.417  
#----------------------  
3.658 4.141  
5.096 2.800  
#######################  
5.293 1.842  
4.518 1.511  
6.215 1.450  
#----------------------  
6.113 3.239  
5.960 3.966  
7.033 2.842  
#######################  
0.658 1.291  
1.919 1.720  
2.404 2.998  
#----------------------  
3.674 3.536  
4.073 3.004  
4.379 4.781  
#----------------------  
3.915 5.534  
3.063 5.270  
4.417 6.366  
#######################  
5.500 5.125  
5.850 4.551  
#----------------------  
5.997 5.958  
0.658 1.291  
#----------------------  
1.899 1.698  
2.455 3.008  
#----------------------  
3.751 3.337  
4.562 3.037  
#######################  
3.962 4.032  
2.945 4.456  
#----------------------  
1.997 4.312  
3.125 4.921  
#----------------------  
5.212 4.220  
5.982 3.896  
#----------------------  
5.386 4.685  
0.658 1.291  
#----------------------  
1.873 1.711  
2.481 2.999  
#######################  
1.556 4.164  
#----------  
0.658 1.291  
#######################  
1.891 1.753  
#----------------------  
0.658 1.291  
#----------------------  
1.919 1.720  
#######################  
0.658 1.291  
1.841 1.793  
1.894 1.773  
#######################  
0.658 1.291  
1.894 1.773  
2.486 3.006  
#----------------------  
3.032 2.690  
3.045 1.710  
3.546 3.682  
#----------------------  
3.561 4.989  
3.219 5.337  
3.915 5.627  
#######################  
3.995 3.220  
3.982 2.227  
#---------------------  
4.348 3.863  
1.467 4.890  
#---------------------  
1.467 4.890  
2.201 4.539  
#---------------------  
2.842 3.762  
2.115 5.051  

我知道sub_objs的定义有问题。但我不知道是什么。为了在公共论坛上发布,我不得不稍微修改变量和代码。请记住与此有关的任何错误并提供帮助!

OBJECTS构造函数中有以下行:

std::vector<SUBOBJECTS> sub_objs(number,size);

声明了一个名为sub_objs局部变量。一旦析构函数退出,这个变量就会超出作用域。它也与OBJECTS::sub_objs 成员变量没有任何关系。

所以你从来没有在任何地方初始化OBJECTS::sub_objs成员变量,这意味着它的值将是不确定的,以任何方式使用它都会导致未定义的行为和你看到的崩溃。

有两个解决方案:

  1. 由于成员变量是一个指针,您需要使用new[]为它分配内存。

  2. 将成员变量的类型改为vector,并在OBJECTS构造函数初始化列表中构造。

这部分是做什么的呢?

struct SUBOBJECTS
{
 SUBOBJECTS(int size)
 {
  coords = new double[size][2]; // [2]
 }
 double (*coords)[3]; // [3]
};

声明了一个包含3个双精度体的数组,然后创建了一个包含2个双精度体的数组。这可能会导致严重的问题。

如果num的size为10,则分配20个双床空间,但声明为30个双床空间。