【发布时间】:2016-11-18 18:22:17
【问题描述】:
所以我在读到clang是一个更严格的标准实现,但这真的让我感到困惑......
void readInputFile(const string inputFileName, Geometry &planeGeom, FlightConditions &FC, AircraftChar & aeroChar,
vector<string> &runFileResults, int &vorlaxRun, int &lateralTrue)
{
int inputRuns;
char infile[32], blank, cTest[32], iTest[32];
char aircraftInpFile[80];
char vorlaxCommand[80];
string casTest, inpTest;
double S_ref,span,Cbar,Xbar,Zbar,St_ref,mac_t,Sf_ref,CD_0,Mach,Uinf,rho;
FILE *ofpInput;
strcpy(infile,inputFileName.c_str());
ofpInput = fopen(infile, "r");
assert(ofpInput!=NULL);
fscanf(ofpInput, "%i", &lateralTrue); //0--false only longitudinal analysis, 1--true long and lateral
printf("lateralTrue = %i\n",lateralTrue);
fscanf(ofpInput, "%i", &vorlaxRun); //0--false will input aero data, 1--true will input run files and execute vorlax
printf("Are we running Vorlax? -- %i\n",vorlaxRun);
fscanf(ofpInput, "%i", &inputRuns); //for looping to run all the needed vorlax files
printf("inputRuns = %i\n",inputRuns);
runFileResults.resize(2*inputRuns);
cout << "I made it to the part where I am reading" << vorlaxRun << "\n" << endl;
fscanf(ofpInput, "%s", &blank); //reading in data from input file
fscanf(ofpInput, "%lf %s", &planeGeom.S, &blank);
fscanf(ofpInput, "%lf %s", &planeGeom.b, &blank);
fscanf(ofpInput, "%lf %s", &planeGeom.cBar, &blank);
fscanf(ofpInput, "%lf %s", &planeGeom.xBar, &blank);
fscanf(ofpInput, "%lf %s", &planeGeom.zBar, &blank);
fscanf(ofpInput, "%lf %s", &planeGeom.St, &blank);
fscanf(ofpInput, "%lf %s", &planeGeom.mact, &blank);
fscanf(ofpInput, "%lf %s", &planeGeom.Sf, &blank);
fscanf(ofpInput, "%lf %s", &aeroChar.CD_0, &blank);
fscanf(ofpInput, "%lf %s", &aeroChar.e, &blank);
fscanf(ofpInput, "%lf %s", &FC.mach, &blank);
fscanf(ofpInput, "%lf %s", &FC.uinf, &blank);
fscanf(ofpInput, "%lf %s", &FC.rho, &blank);
fscanf(ofpInput, "%lf %s", &FC.alpha, &blank);
fscanf(ofpInput, "%lf %s", &aeroChar.dEdA, &blank);
cout << "I made it past reading all the planeGeom stuff " << vorlaxRun << "\n" << endl;
//Commented out code section for debugging purposes...
cout << "I made it past reading all the aeroChar stuff" << "\n" << endl;
所以看起来 int vorlaxRun 导致问题的原因很奇怪,所以我将它添加到我的打印中(如您在上面看到的)以查看发生了什么,这就是我得到的输出。
Reading inputFileMavericAftCGApproachFlapsDown.dat
lateralTrue = 0
Are we running Vorlax? -- 0
inputRuns = 0
I made it to the part where I am reading0
Segmentation fault: 11
如果我取出正在打印的第二个 vorlaxRun 实例,这就是我得到的
Reading inputFileMavericAftCGApproachFlapsDown.dat
lateralTrue = 0
Are we running Vorlax? -- 0
inputRuns = 0
I made it to the part where I am reading0
I made it past reading all the planeGeom stuff
I made it past reading all the aeroChar stuff
Abort trap: 6
我想我会期待中止陷阱 6,因为在查看正在发生的事情的过程中我不得不注释掉一堆行。但我不明白为什么删除将变量打印到屏幕的第二个实例会导致它神奇地工作......或者为什么如果我将它添加到最后一个 cout 上然后我会得到这个:
Reading inputFileMavericAftCGApproachFlapsDown.dat
lateralTrue = 0
Are we running Vorlax? -- 0
inputRuns = 0
I made it to the part where I am reading0
I made it past reading all the planeGeom stuff
Segmentation fault: 11
希望我已经提供了足够的信息。我对clang完全陌生,在谷歌搜索和搜索之后,我在g ++中从未遇到过任何问题我仍然迷路......
【问题讨论】:
-
为什么要将 C 代码与 C++ 混合使用?
-
编译所有警告和调试信息(例如
g++ -Wall -g或clang -Wall -g)。我没有阅读你的代码,但我猜你有一些undefined behavior。使用gdb调试器 -
fscanf(ofpInput, "%s", &blank);很可能是undefined behavior -
阅读您正在调用的每个函数的文档,尤其是en.cppreference.com/w/cpp/io/c/fscanf
-
也坚持使用 C 或 C++ 代码——尤其是在涉及 IO 时。恕我直言,这两种机制不能很好地协同工作。
标签: c++ segmentation-fault g++ clang