我仍然是编程新手。我正在编写2D Snell法律程序。我知道问题可能是由于Xcode中的错误本地化,但我只是用C ++编写,g ++甚至在编译成功后给出了分段错误错误。
以下是我的主要功能代码:
<div class="item" ng-class="{'active': $index===0}" ng-repeat="slide in slides">
<img src="{{slide.image}}" alt="Chania" width="460" height="345">
</div>
以下是Snell.hpp的代码
#include <string>
#include "Snell.hpp"
int main(int argc, const char * argv[]){//thread 1 exc_bad_access (code=2 address=0x7fff5f238304)
string filename;
double time;
Snell S[3600];
for (int i=1; i<=1; i++) {
while (S[i].angle_tr>0) {
filename="VPVSMOD"+to_string(i)+".txt";
S[i].Open(filename);
time=S[i].Locate(i);
cout<<"The "<<i<<"th event takes "<<time<<" seconds to reach the destination"<<endl;
S[i].angle_tr-=0.01;
}
}
return 0;
}
和Snell.cpp:
#ifndef Snell_hpp
#define Snell_hpp
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <cmath>
using namespace std;
class Snell{
private:
double GetV(double lat,double dep);
int ny,nz,time;
double la[30],h[20],v[10][30];
double lat,alt,step;
public:
Snell();
void Open(string filename);
double Locate(int i);
double angle_tr;
};
#endif /* Snell_hpp */
答案 0 :(得分:1)
我的直接猜测是:你必须吹掉你的筹码。请参阅why is stack memory size so limited?
....是的,在我的平台上,我的猜测是正确的......
重播您的计划,但修改您的main.cpp
...
int main(int argc, const char * argv[]){//thread 1 exc_bad_access (code=2 address=0x7fff5f238304)
string filename;
double time;
//Snell S[3600];
std::cout << sizeof(Snell) << " bytes" << std::endl;
return 0;
}
它输出
2848 bytes
....你正试图分配3600
他们......〜10MB
!!
解决方案是使用std::unique_ptr
或更好的好朋友std::vector
在堆上分配它。
将您的主页修改为此
#include <string>
#include <memory>
//or #include <vector>
#include "Snell.hpp"
int main(int argc, const char * argv[]){//thread 1 exc_bad_access (code=2 address=0x7fff5f238304)
string filename;
double time;
std::unique_ptr<S[]> p(new Snell[3600]);
//or std::vector<Snell> S(3600);
for (int i=1; i<=1; i++) {
while (S[i].angle_tr>0) {
filename="VPVSMOD"+to_string(i)+".txt";
S[i].Open(filename);
time=S[i].Locate(i);
cout<<"The "<<i<<"th event takes "<<time<<" seconds to reach the destination"<<endl;
S[i].angle_tr-=0.01;
}
}
return 0;
}