c++ - 主方法声明处的 EXC_BAD_ACCESS

标签 c++ segmentation-fault exc-bad-access

我正在尝试启动并运行一些旧的 C++ 代码。我已经让它编译没有错误,但是当我运行时它立即出现段错误,而没有输入 main。当我使用 gdb 找出问题出在哪里时,我发现了以下内容:

(gdb) run
Starting program: /Users/dreens/Documents/OH/extrabuncher2/ParaOHSB 
Reading symbols for shared libraries +++. done

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x00007fff5636581c
0x000000010000151e in main (argc=1, argv=0x100000ad0) at ParaMainOHSlowerBuncher.cc:13
13  int main(int argc, char *argv[]){
(gdb) backtrace
#0  0x000000010000151e in main (argc=1, argv=0x100000ad0) at ParaMainOHSlowerBuncher.cc:13
(gdb) 

有谁知道在 main 方法开始时可能导致内存访问问题的原因是什么?

代码比较大,但这里是包含主要方法的文件。包含的 .hh 和 .cc 文件是否是问题的一部分?我应该附加它们吗?

谢谢! 大卫

#include <iostream>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include "MoleculeEnsemble.hh"
#include "SlowerForceLoadOH32.cc"
#include "SlowerForceLoadOH12.cc"
//#include "SlowerForceLoad3mmBuncher.cc"
#include "SlowerForceLoad4mmBuncher.cc"

using namespace std;

int main(int argc, char *argv[]){
//int main(){

cout << "Ahhhh!" << endl;
/******Parallel Crap********/
/*
int totalnodes = 0;
int mynode = 0;

MPI_Status status;
MPI_Init(&argv,&argc);
MPI_Comm_size(MPI_COMM_WORLD,&totalnodes);
MPI_Comm_rank(MPI_COMM_WORLD,&mynode);

srand(time(NULL)*mynode);

*/
/******Distribution Parameters *******/
long MoleculeNumber = long(5e4);
double Xcenter = 0;
double Ycenter = 0;
double Zcenter = 0;
double DeltaX = 0.0015;
double DeltaY = 0.0015;
double DeltaZ = 0.01;
int FlatX = 1;
int FlatY = 1;
int FlatZ = 1;
double vXcenter = 0;
double vYcenter = 0;
double vZcenter = 406;
double Vcalc = 406;
double vZfinal = 0;
double DeltavX = 2;
double DeltavY = DeltavX;
double DeltavZ = 40;
int FlatvX = 0;
int FlatvY = 0;
int FlatvZ = 0;

int TimeArrayOnly = 0; //Outputs only Time Array
double TimeOffset = 0; //Adds valve-skimmer flight time to ToF array

/*******Overtone Parameters********/
int S = 1;      //parameter S=Vz/Vswitch as defined by VDM et al.
int JILAOT = 0; //JILAOT is either 0 or 1, denoting whether or not to use special switching

/*******Hexapole Parameters********/
double VSD = 0.06;
double Voltage = 2000;
double HexRadius = .00268;
double HexStart = .0238;
double HexEnd = .083170;//0.089103;
double HexOn = 1e-6;
double HexOff = 203e-6;//224e-6; 212 for current data; Good = 243e-6 for 408m/s
double DeltaT = 1e-6;
double DeltaTSeqGen = 1e-9; //Need to use smaller time steps for finding the time sequence
double DetectionTime = HexOff; //Use to fake out hex code
double TriggerLatency = 0;//170e-9;

/*******Detection Parameters*******/
double DetectionPosition = double(0.9319);//0.257480; <- for viewing at 31.5 |||||   0.9428; <-Mag trap(4stages), .9319 <-MagTrap(3Stages)
double IrisWidth = 0.008;//31.5 0.0023 //PostSlower.015;
double LaserRadius = .001;

/*****Bunching Paramaters******/
int BunchNumber = 0;
int NumberUsed = 0;

/*****Timing Variables*********/
time_t start, finish;
time( &start);

/*****Molecule Parameters******/
double mass =double(17*1.672e-27);


/******ToF Detection Arrays and Slowing Parameters *********/
double Phi = double(34.2);
double PhiEB = double(0);
int NumberOfStages = int(142/S); //Use 142 for Big machine
int EBStages = 3;                //Larger Add-on stages at end of slower
double BuncherScale = 1;
double Time[int(1e7)];
int ToFSignal32[int(1e7)];
int ToFSignal12[int(1e7)];
double TimeArray[800];
double VExit[800];
double Average32[7];
double Average12[7];
int LOST[200];
/*************Finished ToF Detection Arrays and Slowing Parameters ********/



/******Force Arrays********/
int Xnumber = 111;
int Ynumber = 21;
int Znumber = 21;
int FLength = Xnumber*Ynumber*Znumber;
double AXxDT[FLength];
double AYxDT[FLength];
double AZxDT[FLength];
double AZxDTSeqGen[FLength];
SlowerForceLoadOH32(AZxDT, AYxDT, AXxDT); //Note how Z and X are placed in this function. My matlab code calls the longitudnal dimension X, here it is Z
double DTovermass = DeltaT/mass;

for(int j = 0; j <FLength; j++){
    AXxDT[j] = DTovermass*AXxDT[j];
    AYxDT[j] = DTovermass*AYxDT[j];
    AZxDT[j] = DTovermass*AZxDT[j];
        AZxDTSeqGen[j] = DeltaTSeqGen*AZxDT[j]/DeltaT;
}


double AXxDT12[FLength];
double AYxDT12[FLength];
double AZxDT12[FLength];

SlowerForceLoadOH12(AZxDT12, AYxDT12, AXxDT12); //Note how Z and X are placed in this function. My matlab code calls the longitudnal dimension X, here it is Z
for(int j = 0; j <FLength; j++){    
        AXxDT12[j] = DTovermass*AXxDT12[j];
        AYxDT12[j] = DTovermass*AYxDT12[j];
        AZxDT12[j] = DTovermass*AZxDT12[j];
}

/********Load Extra Buncher Forces*********/
int XnumberEB = 251;
int YnumberEB = 41;
int ZnumberEB = 41;
int FLengthEB = XnumberEB*YnumberEB*ZnumberEB;
double AXxDTEB[FLengthEB], AYxDTEB[FLengthEB], AZxDTEB[FLengthEB], AZxDTSeqGenEB[FLengthEB];
SlowerForceLoad4mmBuncher(AZxDTEB, AYxDTEB, AXxDTEB);

for(int j = 0; j <FLengthEB; j++)
{
        AXxDTEB[j] = DTovermass*AXxDTEB[j]/BuncherScale;
        AYxDTEB[j] = DTovermass*AYxDTEB[j]/BuncherScale;
        AZxDTEB[j] = DTovermass*AZxDTEB[j]/BuncherScale;
        AZxDTSeqGenEB[j] = DeltaTSeqGen*AZxDTEB[j]/(DeltaT*BuncherScale);
}




/********* End All initiliazation ***************************/


/************Beginning Calculation *************************/
//Create Molecule Ensemble
MoleculeEnsemble Alice(MoleculeNumber,Xcenter,Ycenter,Zcenter,DeltaX,DeltaY,DeltaZ,FlatX,FlatY,FlatZ,vXcenter,vYcenter,vZcenter,DeltavX,DeltavY,DeltavZ,FlatvX,FlatvY,FlatvZ);
//MoleculeEnsemble Bob(MoleculeNumber,Xcenter,Ycenter,Zcenter,DeltaX,DeltaY,DeltaZ,FlatX,FlatY,FlatZ,vXcenter,vYcenter,vZcenter,DeltavX,DeltavY,DeltavZ,FlatvX,FlatvY,FlatvZ);

//Generate the Timing Sequence
Alice.TimeArrayGeneratorWithBuncher(Vcalc,Phi,PhiEB,TimeArray,VExit,AZxDTSeqGen,AZxDTSeqGenEB,HexOff,DeltaTSeqGen,BunchNumber,vZfinal,NumberUsed,NumberOfStages,S,EBStages);

/*if(mynode == 0){
cout << "Slowing utilized " << NumberUsed << " stages, yielding a final velocity of " << VExit[NumberUsed] << " m/s." << endl;
cout << endl;
for(int kk = 0; kk < NumberOfStages; kk++){cout << kk << " , " << TimeArray[kk] << " , " << VExit[kk] << endl;}
}*/

/*Alice.MoleculeEnsemble_Averager(Average32);
Bob.MoleculeEnsemble_Averager(Average12);

cout << "Processor: " << mynode << "\t" << sqrt(pow(Average32[3],2)+pow(Average32[4],2)) << ", " << sqrt(pow(Average12[3],2)+pow(Average12[4],2));
cout << " Mean = " << Average32[6] << ", " << Average12[6] << endl << endl << endl;
*/

if(TimeArrayOnly!=1)
{
    //Fly the Ensemble through the hexapole
    Alice.HexapoleFlightOH(Voltage, HexRadius, HexStart, HexEnd, HexOn, HexOff, DeltaT, double(3/2), DetectionTime); 
    //Bob.HexapoleFlightOH(Voltage, HexRadius, HexStart, HexEnd, HexOn, HexOff, DeltaT, double(1/2), DetectionTime);

    /*
    Alice.MoleculeEnsemble_Averager(Average32);
    Bob.MoleculeEnsemble_Averager(Average12);

    cout << "Processor: " << mynode << "\t" << sqrt(pow(Average32[3],2)+pow(Average32[4],2)) << ", " << sqrt(pow(Average12[3],2)+pow(Average12[4],2));
    cout << " Mean = " << Average32[6] << ", " << Average12[6] << endl << endl << endl;
    */

    //Fly the Ensemble through the slower 
    Alice.SlowerFlight(LOST, Time, ToFSignal32, Phi, TimeArray, DeltaT, AXxDT, AYxDT, AZxDT, AXxDTEB, AYxDTEB, AZxDTEB, Xnumber, Ynumber, Znumber, DetectionPosition, IrisWidth, LaserRadius, NumberOfStages, EBStages,S, TriggerLatency);
    //Bob.SlowerFlight(LOST, Time, ToFSignal12, Phi, TimeArray, DeltaT, AXxDT12, AYxDT12, AZxDT12, Xnumber, Ynumber, Znumber, DetectionPosition, IrisWidth, LaserRadius, NumberOfStages, EBStages, S, TriggerLatency);

}

/**********Ending Calculation **********************/


//Alice.MoleculeEnsemble_Drawer();

/*
Alice.MoleculeEnsemble_Averager(Average32);
Bob.MoleculeEnsemble_Averager(Average12);

cout << "Processor: " << mynode << "\t" << sqrt(pow(Average32[3],2)+pow(Average32[4],2)) << ", " << sqrt(pow(Average12[3],2)+pow(Average12[4],2)); 
cout << " Mean = " << Average32[6] << ", " << Average12[6] << endl << endl; 
*/


//Output ToF signal

if(TimeArrayOnly!=1)
{
    for(int ii = 0; ii < int(1e7); ii++)
    {
    if(ToFSignal32[ii] > 0 && Time[ii] > 3e-3)
        {
       cout << Time[ii]+TimeOffset << "," << ToFSignal32[ii] << endl;
           //+double(VSD/vZcenter)+38e-6 << "," << ToFSignal32[ii] << endl;
    }

        if(ToFSignal12[ii] > 0 && Time[ii] > 3e-3)
        {
           cout << Time[ii]+TimeOffset << "," << ToFSignal12[ii] << endl;
           //+double(VSD/vZcenter)+38e-6 << "," << ToFSignal12[ii] << endl;
        }

    }
}


if(TimeArrayOnly==1)
{
    for(int ii = 0; ii < NumberOfStages+EBStages+1; ii++)
    {
        cout << ii << "\t" << TimeArray[ii] << "\t" << VExit[ii]  << endl;
        //+double(VSD/vZcenter)+double(265e-6) << "\t" << VExit[ii]  << endl;
    }
}

/*for(int ii = 0; ii < NumberOfStages; ii++)
{
        cout << ii << "\t" << LOST[ii] << endl;
}
*/


/*        
MPI_Finalize();
*/



}

最佳答案

你的堆栈空间不足。

您在代码中声明了非常大的数组(超过 1000 万个元素),这些数组都分配在堆栈上。不要静态声明数组,而是使用动态内存分配。所以,而不是

double Time[int(1e7)];

double* Time;
Time = new double[int(1e7)];

并希望您的电脑有足够的 RAM :)

关于c++ - 主方法声明处的 EXC_BAD_ACCESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20314489/

相关文章:

python - 在 C++ 中使用 Python 对象

c++ - QNetworkAccessManager 中的并行化机制是什么?

C++ 线程附加/分离段错误

c++ - 函数递归的最大次数c++

macos - 添加 SKSpriteNode 时 Swift 中的 EXC_BAD_ACCESS

c - C中简单字符串复制函数中的BAD_ACCESS_ERR

c++ - 在 Release模式下,代码行为不如预期

c - 如果复制字符串数组,写入套接字会导致段错误

java - Mapbox Android SDK - 致命信号 11 (SIGSEGV)

objective-c - Objective C - [NSArray count] 上的 EXC_BAD_ACCESS 但看不到如何