作者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