问题
I have a big project which includes several source files and header files. to put some variables together, I have used struct
. However, time to time I get SIGABRT
error for various arrays in my project. I have checked out net a little, and I figured out that I think I have to turn them to the <vector>
. If you guys have another ideas, I really like to hear them. This is the error event:
http://i.hizliresim.com/gnGDdR.png
Here is my structs and their source files:
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
I defined them in the tools.h and first I have used in the tools.cpp as globally:
cognitiveRadio myCR;
Graph myGraph;
timeMatrix myMatrix;
However, I need to use them in other sources files too. So, I added the following lines in each other source files:
extern struct Graph myGraph;
extern struct cognitiveRadio myCR;
extern struct timeMatrix myMatrix;
I allocated them like this: (this one is in tools.cpp)
myMatrix.value=new double*[PU_number];
for( i=0;i<PU_number;i++)
myMatrix.value[i]=new double[time_slots];
There are also another arrays in project, like this:
void orderDegree(int CR_Number)
{
int *degreeOrdered;
degreeOrdered=new int[CR_Number];
....
}
Here are the other header files:
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
In main.cpp, I have this:
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;
}
Well, as you can see it is highly complex. But simply, order of call for functions is like this:
main.cpp calls
HyperHeuristic()
from hyperheuristic.cpp you see abovehyperheuristic()
callsAICS()
in the same source fileAICS()
calls `executeLLHorder()̀ in the same source fileexecuteLLHorder()
calls some Low level heuristics which you can see them in the ̀̀hyperheuristic.hthen again,
executeLLHorder()
callsfitnesscalc()
from ̀fitness.cpp
However, I told before, sometimes it works perfectly, but sometimes it returns SIGABRT
error for various arrays in the project. There is not any certain array. It just does it. What is the best way to allocate these arrays? especially arrays that in the struct
s ?
回答1:
I think I have to turn them to the vector
Your code is error prone, and these days its generally recommended not to use naked new
and delete
. You should wrap your allocations in a vector<T>
, unique_ptr<T>
, or shared_ptr<T>
.
For example this:
struct timeMatrix {
double **value;
};
myMatrix.value=new double*[PU_number];
for( i=0;i<PU_number;i++)
myMatrix.value[i]=new double[time_slots];
Could become this:
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);
This:
void orderDegree(int CR_Number)
{
int *degreeOrdered;
degreeOrdered=new int[CR_Number];
....
}
Could become this:
void orderDegree(int CR_Number)
{
std::unique_ptr<int[]> degreeOrdered;
degreeOrdered.reset(new int[CR_Number]);
....
}
Once you've wrapped your allocations in objects with automatic storage duration, you'll be in a better position to find your bugs. For example, with vector<T>
you will get failed assertions if you try to access an element that is out of range.
http://www.cplusplus.com/reference/vector/vector/ http://www.cplusplus.com/reference/memory/unique_ptr/ http://www.cplusplus.com/reference/memory/shared_ptr/
来源:https://stackoverflow.com/questions/28377741/sigabrt-in-arrays-c-how-to-turn-to-vector