c++ - 数组中的 SIGABRT C++ 如何转向 <vector>

标签 c++ arrays vector sigabrt

我有一个大项目,其中包含几个源文件和头文件。为了将一些变量放在一起,我使用了 struct .但是,我有时会得到 SIGABRT我项目中各种数组的错误。我检查了一下网络,发现我认为我必须将它们转到 <vector> .如果你们有其他想法,我真的很想听听。这是错误事件:

http://i.hizliresim.com/gnGDdR.png

这是我的结构和它们的源文件: tools.h :

#ifndef _TOOLS_H_
#define _TOOLS_H_

void initialization(int,int,int);
int createGraph(int);
void orderDegree(int);
int nextTime(float lambda,int timeslots,int PU_number);

struct cognitiveRadio
{
    int **followed;
    double *priority;
    double *demand;
    int *degree;
    bool *assigned;
};

struct Graph
{
    int **adj;
    int *degree;
};
struct timeMatrix
{
    double **value;
};
#endif

我在 tools.h 中定义了它们,首先我在 tools.cpp 中全局使用了它们:

cognitiveRadio myCR;
Graph myGraph;
timeMatrix myMatrix;

但是,我也需要在其他源文件中使用它们。因此,我在彼此的源文件中添加了以下行:

extern struct Graph myGraph;
extern struct cognitiveRadio myCR;
extern struct timeMatrix myMatrix;

我是这样分配它们的:(这个在 tools.cpp 中)

myMatrix.value=new double*[PU_number];
for( i=0;i<PU_number;i++)
    myMatrix.value[i]=new double[time_slots];

项目中还有另外一个数组,像这样:

void orderDegree(int CR_Number)
{
    int *degreeOrdered;
    degreeOrdered=new int[CR_Number];
....
}

其他头文件如下:

HyperHeuristic.h :

#ifndef _HYPERHURISTIC_H_
#define _HYPERHURISTIC_H_
#include "tools.h"
/*********Hyper-Heuristics*************/
double hyperheuristic(int ColumnIndex,int CR_Number,int PU_number,int hh,int accCriteria,int ts);
double simple_random(int ColumnIndex,int CR_Number,int PU_number,int ts);
double AICS(int ColumnIndex,int CR_Number,int PU_number,int ts);
double executeLLHorder(int ColumnIndex,int *LLHorder,int CR_Number,int PU_number,int ts);
/*********Low Level Heuristics***********/
int *largestDegree(int ColumnIndex,int CR_Number,int PU_number, Graph myGraph,int **CRs,int *PUs);
int *maxRadius(int ColumnIndex,int CR_Number,int PU_number, Graph myGraph,int **CRs,int *PUs);
int *maxDemand(int ColumnIndex,int CR_Number,int PU_number, Graph myGraph,int **CRs,int *PUs);
int *maxPriority(int ColumnIndex,int CR_Number,int PU_number, Graph myGraph,int **assignmentTable,int *availablePUs);
int *justRandom(int ColumnIndex,int CR_Number,int PU_number, Graph myGraph,int **CRs,int *PUs);
void print(double **arr);
struct ACO
{
    int *LLH;
    double deltatau;
};
struct AICStools
{
    double **probability;
    double **tau;
    double *roulette_wheel;
};

#endif // _HYPERHURISTIC_H_

fitness.h :

#ifndef _FITNESS_H_
#define _FITNESS_H_

double fitnessCalc(int**CRs,int CR_Number,int ColumnIndex,int PU_Number,int ts);
#endif

在 main.cpp 中,我有这个:

int time_slots=nextTime(1,number_of_packets,PU_number);
initialization(PU_number,CR_Number,time_slots);
for(int i=0;i<2;i++)
{
    double x=hyperheuristic(i,CR_Number,PU_number,hh,accCriteria,time_slots);
    cout<<"Fitness value of time("<<i<<"): "<<(double)x<<endl;
}

好吧,如您所见,它非常复杂。但简单来说,函数的调用顺序是这样的:

  • main.cpp 调用 HyperHeuristic()来自上面的 hyperheuristic.cpp

  • hyperheuristic()电话 AICS()在同一个源文件中

  • AICS()在同一源文件中调用 `executeLLHorder()̀

  • executeLLHorder()调用一些低级启发式算法,您可以在 ̀̀hyperheuristic.h 中看到它们

  • 然后,executeLLHorder()电话 fitnesscalc()来自 ̀fitness.cpp

但是,我之前说过,有时它运行得很好,但有时它返回 SIGABRT项目中各种数组的错误。没有任何特定的数组。它就是这样做的。分配这些数组的最佳方式是什么?特别是 struct 中的数组是吗?

最佳答案

I think I have to turn them to the vector

您的代码容易出错,现在一般建议不要使用裸newdelete .您应该将分配包装在 vector<T> 中, unique_ptr<T> , 或 shared_ptr<T> .

例如这个:

struct timeMatrix {
    double **value;
};

myMatrix.value=new double*[PU_number];
for( i=0;i<PU_number;i++)
    myMatrix.value[i]=new double[time_slots];

可以变成这样:

struct timeMatrix {
    std::vector< std::vector<double> > values;
};

myMatrix.values.resize(PU_number);

for(i = 0; i < PU_number; i++)
    myMatrix.values[i].resize(time_slots);

这个:

void orderDegree(int CR_Number)
{
    int *degreeOrdered;
    degreeOrdered=new int[CR_Number];
....
}

可以变成这样:

void orderDegree(int CR_Number)
{
    std::unique_ptr<int[]> degreeOrdered;
    degreeOrdered.reset(new int[CR_Number]);
....
}

一旦您将分配包装在具有自动存储持续时间的对象中,您就可以更好地找到错误。例如,使用 vector<T>如果您尝试访问超出范围的元素,您将得到失败的断言。

http://www.cplusplus.com/reference/vector/vector/ http://www.cplusplus.com/reference/memory/unique_ptr/ http://www.cplusplus.com/reference/memory/shared_ptr/

关于c++ - 数组中的 SIGABRT C++ 如何转向 <vector>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28377741/

相关文章:

c++ - 在头文件的静态成员函数内声明了多少个静态数据实例?

c++ - 如何转发声明 boost::ptree::iterator

c++ - 类的继承变量和自身变量的区别

c++ - 类方法变量,如果我将它们存储在类本身中会更快吗?

c# - 在不初始化长度的情况下向数组添加值

c++ - 读取字符串 vector 时出现超出范围的运行时错误

c++ - 使用 OpenCV 描述符匹配 findFundamentalMat

c++ - 为什么 std::vector 是原始数组的两倍?包含完整代码

c++ - 返回对私有(private)成员与公共(public)成员的引用

c - 通过从中删除特定的行和列来缩小二维数组的大小