数组 C++ 中的 SIGABRT 如何转向 <vector>
SIGABRT in arrays c++ how to turn to <vector>
我有一个大项目,其中包含几个源文件和头文件。为了将一些变量放在一起,我使用了 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.cpp 调用 HyperHeuristic()
你在上面看到
hyperheuristic()
在同一源文件中调用 AICS()
AICS()
在同一源文件中调用 `executeLLHorder()̀
executeLLHorder()
调用一些低级启发式方法,您可以在 ̀̀hyperheuristic.h
中看到它们
然后,executeLLHorder()
从 ̀fitness.cpp
调用 fitnesscalc()
但是,我之前说过,有时它完美地工作,但有时它 returns SIGABRT
项目中各种数组的错误。没有任何特定的数组。它就是这样做的。分配这些数组的最佳方式是什么?特别是 struct
s ?
中的数组
I think I have to turn them to the vector
您的代码 容易出错,最近通常建议不要使用裸 new
和 delete
。您应该将分配包装在 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/
我有一个大项目,其中包含几个源文件和头文件。为了将一些变量放在一起,我使用了 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.cpp 调用
HyperHeuristic()
你在上面看到hyperheuristic()
在同一源文件中调用AICS()
AICS()
在同一源文件中调用 `executeLLHorder()̀executeLLHorder()
调用一些低级启发式方法,您可以在 ̀̀hyperheuristic.h 中看到它们
然后,
executeLLHorder()
从 ̀fitness.cpp 调用
fitnesscalc()
但是,我之前说过,有时它完美地工作,但有时它 returns SIGABRT
项目中各种数组的错误。没有任何特定的数组。它就是这样做的。分配这些数组的最佳方式是什么?特别是 struct
s ?
I think I have to turn them to the vector
您的代码 容易出错,最近通常建议不要使用裸 new
和 delete
。您应该将分配包装在 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/