SIGABRT在阵列C ++如何打开到<矢量> [英] SIGABRT in arrays c++ how to turn to <vector>

查看:71
本文介绍了SIGABRT在阵列C ++如何打开到<矢量>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大的项目,其中包括多个源文件和头文件。把一些变量在一起,我已经使用结构。但是,不时我得到 SIGABRT 错误在我的项目不同的阵列。我已签出净一点,我想通了,我想我必须给他们转向<矢量GT; 。如果你们有其他的想法,我真的很喜欢听到他们的声音。这是错误事件:

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

下面是我的结构和它们的源文件:
的Tools.h

 的#ifndef _TOOLS_H_
#定义_TOOLS_H_无效初始化(INT,INT,INT);
INT createGraph(INT);
无效orderDegree(INT);
INT nextTime(浮点拉姆达,诠释时隙,诠释PU_number);结构cognitiveRa​​dio
{
    INT **其次;
    双*优先;
    双*的需求;
    INT *度;
    BOOL *分配;
};结构图
{
    INT **形容词;
    INT *度;
};
结构timeMatrix
{
    双**值;
};
#万一

我在的Tools.h定义它们,首先我在tools.cpp为全球范围内使用:

  cognitiveRa​​dio myCR;
图表为MyGraph;
timeMatrix于myMatrix;

不过,我需要在其他渠道使用这些文件了。所以,我添加了以下行中的每个其他源文件:

 的extern结构图表为MyGraph;
EXTERN结构cognitiveRa​​dio myCR;
EXTERN结构timeMatrix于myMatrix;

我分配他们是这样的:(这是在tools.cpp)

  myMatrix.value =新的双* [PU_number]
对于(i = 0; I< PU_number;我++)
    myMatrix.value [I] =新的双[time_slots]

有项目也是另一个阵列,像这样:

 无效orderDegree(INT CR_Number)
{
    INT * degreeOrdered;
    degreeOrdered =新INT [CR_Number]
....
}

下面是其他头文件:

HyperHeuristic.h

 的#ifndef _HYPERHURISTIC_H_
#定义_HYPERHURISTIC_H_
的#include的Tools.h
/ *********超启发式************* /
双hyperheuristic(INT参数:columnIndex,诠释CR_Number,诠释PU_number,INT 11H,INT accCriteria,INT TS);
双simple_random(INT参数:columnIndex,诠释CR_Number,诠释PU_number,INT TS);
双AICS(INT参数:columnIndex,诠释CR_Number,诠释PU_number,INT TS);
双executeLLHorder(INT参数:columnIndex,为int * LLHorder,诠释CR_Number,诠释PU_number,INT TS);
/ *********低电平启发式*********** /
为int * largestDegree(INT参数:columnIndex,诠释CR_Number,诠释PU_number,图形为MyGraph,INT **的CR,诠释* PUS);
为int * maxRadius(INT参数:columnIndex,诠释CR_Number,诠释PU_number,图形为MyGraph,INT **的CR,诠释* PUS);
为int * maxDemand(INT参数:columnIndex,诠释CR_Number,诠释PU_number,图形为MyGraph,INT **的CR,诠释* PUS);
为int * maxPriority(INT参数:columnIndex,诠释CR_Number,诠释PU_number,图形为MyGraph,INT ** assignmentTable,为int * availablePUs);
为int * justRandom(INT参数:columnIndex,诠释CR_Number,诠释PU_number,图形为MyGraph,INT **的CR,诠释* PUS);
无效打印(双** ARR);
结构ACO
{
    INT * LLH;
    双deltatau;
};
结构AICStools
{
    双**的概率;
    双**头;
    双* roulette_wheel;
};#ENDIF // _HYPERHURISTIC_H_

fitness.h

 的#ifndef _FITNESS_H_
#定义_FITNESS_H_双fitnessCalc(INT **的CR,诠释CR_Number,INT参数:columnIndex,诠释PU_Number,INT TS);
#万一

在main.cpp中,我有这样的:

  INT time_slots = nextTime(1 number_of_packets,PU_number);
初始化(PU_number,CR_Number,time_slots);
的for(int i = 0;我2;我++)
{
    双X = hyperheuristic(I,CR_Number,PU_number,HH,accCriteria,time_slots);
    COUT<<时间健身价值(<< I<<):<<(双)x<< ENDL;
}

那么,你可以看到它是非常复杂的。但是,简单地说,要求功能的顺序是这样的:


  • main.cpp中调用 HyperHeuristic()从hyperheuristic.cpp以上你看到的


  • hyperheuristic()通话 AICS()同一个源文件中的


  • AICS()调用'executeLLHorder()同一个源文件中的


  • executeLLHorder()调用一些低级别的启发式,你可以看到他们在hyperheuristic.h


  • 话又说回来, executeLLHorder()通话 fitnesscalc()从fitness.cpp


不过,我对之前,有时它完美的作品,但有时它会返回 SIGABRT 错误的项目不同的阵列。没有任何特定的阵列。它只是它。什么是分配这些阵列的最佳方式?尤其是阵列,在结构 S'


解决方案

  

我觉得我有他们转向矢量


您code的的容易出错,这几天它通常建议不要使用明火删除。你应该换你分配在矢量< T> 的unique_ptr< T> shared_ptr的< T>

例如对于这样的:

 结构timeMatrix {
    双**值;
};myMatrix.value =新的双* [PU_number]
对于(i = 0; I< PU_number;我++)
    myMatrix.value [I] =新的双[time_slots]

有可能成为这样的:

 结构timeMatrix {
    的std ::矢量<的std ::矢量<&双GT; >值;
};myMatrix.values​​.resize(PU_number);对于(i = 0; I< PU_number;我++)
    myMatrix.values​​ [I] .resize(time_slots);

 无效orderDegree(INT CR_Number)
{
    INT * degreeOrdered;
    degreeOrdered =新INT [CR_Number]
....
}

有可能成为这样的:

 无效orderDegree(INT CR_Number)
{
    的std ::的unique_ptr< INT [] GT; degreeOrdered;
    degreeOrdered.reset(新INT [CR_Number]);
....
}

一旦你的包裹中的分配与自动存储时间的对象,你会在一个更好的位置找到你的错误。例如,矢量< T> 如果您尝试访问超出范围的元素,你会得到失败的断言

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

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 above

  • hyperheuristic() calls AICS() in the same source file

  • AICS() calls `executeLLHorder()̀ in the same source file

  • executeLLHorder() calls some Low level heuristics which you can see them in the ̀̀hyperheuristic.h

  • then again, executeLLHorder() calls fitnesscalc() 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 structs ?

解决方案

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/

这篇关于SIGABRT在阵列C ++如何打开到&lt;矢量&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆