作者peter74123 ()
看板C_and_CPP
标题[问题] 程式记忆体区段错误?
时间Mon Aug 31 19:47:05 2009
小弟是用Linux写程式
编译器是g++
我只是写了一个读档的程式要存data至阵列
编译也都没问题
可是执行 ./a.out 以後却会出现
"程式记忆体区段错误"
请问这是什麽意思
不好意思 我不确定这是程式的问题还是Linux的问题
code
----
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int main()
{
ifstream inFile( "data.dat", ios::in );
if ( !inFile ){
cerr<<"File could not be opened"<<endl;
exit( 1 );
}
ofstream outFile( "3Ddata.dat", ios::out );
if ( !outFile ){
cerr<<"File could not be opened"<<endl;
exit( 1 );
}
double x;
double y;
double z;
double vx;
double vy;
double vz;
int counter = 0;
double xyz[180][180][180]={0};
while ( inFile >> x >> y >> z >> vx >> vy >> vz ){
counter = counter+1;
int xgrid = 90+x/0.04;
int ygrid = 90+y/0.04;
int zgrid = 90+z/0.04;
if(xgrid>=0 && ygrid>=0 && zgrid>=0 && xgrid<180 && ygrid<180 &&
zgrid<180){
xyz[xgrid][ygrid][zgrid] = xyz[xgrid][ygrid][zgrid]+1.0;
}
cout<< counter <<endl;
}
int i,j,k;
for(i=0; i<180; i++){
for(j=0; j<180; j++){
for(k=0; k<180; k++){
xyz[i][j][k]= xyz[i][j][k]*(1.29e+8)/(0.04*0.04*0.04);
outFile<<(i-90)*0.04+0.02<<" "<<(j-90)*0.04+0.02<<" "<<(k-90)*0.04+
0.02<< " "<<xyz[i][j][k]<<endl;
}
}
}
return 0;
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.112.4.235
1F:推 VictorTom:就存取了错误的记忆体吧, 请检查开档是否成功, 阵列是不 08/31 19:49
2F:→ VictorTom:是有合法的记忆体空间; 最後, 该贴code就要贴code, 十 08/31 19:50
3F:→ VictorTom:之八九都是PG自己把程式写坏, OS或硬体错的机会少得很. 08/31 19:50
贴了
※ 编辑: peter74123 来自: 140.112.4.235 (08/31 20:16)
4F:推 VictorTom:xyz阵列改用动态配置的试试, 我猜stack爆了那个xyz阵列 08/31 20:38
5F:→ VictorTom:根本没得用; 或者知道怎麽调的话去改stack的size试试; 08/31 20:39
6F:→ VictorTom:total 用了 0x2C7EA00 这麽大的stack memory....@_@" 08/31 20:39
7F:推 LiloHuang:请爱用 gdb 进行除错 或者把 core dump 打开即可 08/31 22:48