关于循环指针时地址出错的问题
sdata.zip
(27.85 KB)
[attach]65520[/attach]各位高手:有个问题想请教,
我用指针循环的处理数据时,指针变量出错,不知道原因是什么?代码如下:
程序代码:
#include <iostream> #include <fstream> #include <ctime> using namespace std; int main() { long start,finish; start=clock(); ifstream sfile("sdata.txt",ios::in); const unsigned short row=1720,col=60; unsigned short r,c; unsigned short s[row][col]; for(r=0;r<row;r++) for(c=0;c<col;c++) sfile>>s[r][c]; sfile.close(); unsigned short (*sp)[col]; sp=s; unsigned short res[100][19]; unsigned short (*rp)[19]; rp=res; unsigned short a1,b1,c1,d1,e1,f1,r1,x,y,z,cln=0; unsigned short col_6=col/6; //把整个二维数组按列平均分成6部分 const unsigned short cptimes=3; unsigned short t[64]; unsigned short *tp; tp=t; for(a1=0;a1<col_6;a1++) for(b1=col_6;b1<2*col_6;b1++) for(c1=2*col_6;c1<3*col_6;c1++) for(d1=3*col_6;d1<4*col_6;d1++) for(e1=4*col_6;e1<5*col_6;e1++) for(f1=5*col_6;f1<6*col_6;f1++) { memset(tp,0,64); for(r1=0;r1<row-3;r1++) { z=*(*(sp+r1)+a1)*32+*(*(sp+r1)+b1)*16+*(*(sp+r1)+c1)*8+*(*(sp+r1)+d1)*4+*(*(sp+r1)+e1)*2+*(*(sp+r1)+f1);//运行时此行出错,f1值达到574,超出要求 *(tp+z)+=1; } for(z=0;z<64;z++) { if(*(tp+z)<cptimes) //结果存入res二维数组 { cln+=2; res[cln+1][18]=*(tp+z); res[cln+1][0]=*(*(sp+row-3)+a1);res[cln+1][1]=*(*(sp+row-2)+a1);res[cln+1][2]=*(*(sp+row-1)+a1); res[cln+1][3]=*(*(sp+row-3)+b1);res[cln+1][4]=*(*(sp+row-2)+b1);res[cln+1][5]=*(*(sp+row-1)+b1); res[cln+1][6]=*(*(sp+row-3)+c1);res[cln+1][7]=*(*(sp+row-2)+c1);res[cln+1][8]=*(*(sp+row-1)+c1); res[cln+1][9]=*(*(sp+row-3)+d1);res[cln+1][10]=*(*(sp+row-2)+d1);res[cln+1][11]=*(*(sp+row-1)+d1); res[cln+1][12]=*(*(sp+row-3)+e1);res[cln+1][13]=*(*(sp+row-2)+e1);res[cln+1][14]=*(*(sp+row-1)+e1); res[cln+1][15]=*(*(sp+row-3)+f1);res[cln+1][16]=*(*(sp+row-2)+f1);res[cln+1][17]=*(*(sp+row-1)+f1); } } } ofstream rfile("result.txt",ios::out); //输出部分 for(x=0;x<100;x++) //预多行数来输出 for(y=0;y<19;y++) { rfile<<res[x][y]<<'\t'; if(y==18) rfile<<endl; } sfile.close(); double duration; finish=clock(); duration=(double)(finish-start)/CLOCKS_PER_SEC; cout<<"duration="<<duration<<endl; return 0; }在运行调试时发现指针变量f1的值超出设计要求(50-59)的值达到(574),这是否与txt文档的格式限制有关?
因为原始数据是从EXCEL2010导出的1720行60列的二维数组,我觉得在txt文档中显示时有点怪,是否是被txt强制换行了?
我也把原始数据上传了,请各位高手帮忙分析一下,指点迷津!
谢谢!
[ 本帖最后由 HenryLin8 于 2012-10-15 10:47 编辑 ]