[转,]
摘要 Segmentation fault 一般有是以下原因: 1)访问系统数据区,尤其是往系统保护的内存地址写数据最常见就是给一个指针以0地址。 2)内存越界(数组越界,变量类型不一致等): 访问到不属于你的内存区域。
1 #include2 #include 3 #include 4 #include 5 #include 6 7 #define ROW 370 8 #define COL 740 9 #define pi 3.1415926 10 #define times 10 //码流组数 11 #define LOOP 50 //每个DB值循环次数 12 //------------------------------------------------------------------- 13 //------------------------------------------------------------------- 14 typedef unsigned char UINT8; 15 typedef unsigned int UINT16; 16 typedef unsigned int UINT32; 17 typedef char INT8; 18 typedef int INT16; 19 typedef int INT32; 20 //------------------------------------------------------------------- 21 void genH(INT16 row,INT16 col); 22 //------------------------------------------------------------------- 23 void showH(); 24 //------------------------------------------------------------------- 25 void encode(bool s[],INT8 H[][COL],bool input[]); 26 //------------------------------------------------------------------- 27 void bpsk(bool input[],INT16 amp,INT16 tx_waveform[]); 28 //------------------------------------------------------------------- 29 void awgn(INT16 tx_waveform[],double rx_waveform[],INT16 length,double SNR); 30 //------------------------------------------------------------------- 31 void wgn(INT16 length,double noisepower,double noise_s[]); 32 //------------------------------------------------------------------- 33 void randn(double noise[],INT16 length); 34 //------------------------------------------------------------------- 35 void bp_decode(double rx_waveform[],double SNR_db,INT16 amp,INT8 H[][COL], 36 INT16 rearranged_cols[],bool output[]); 37 //------------------------------------------------------------------- 38 double atanh(double x); 39 //------------------------------------------------------------------- 40 INT16 sgn(double x); 41 struct original_bp 42 { 43 float qmn0; 44 float qmn1; 45 float dqmn; 46 float rmn0; 47 float rmn1; 48 float qn0; 49 float qn1; 50 float alpha; 51 }; 52 //------------------------------------------------------------------- 53 static INT8 H[ROW][COL]={ 0}; 54 //------------------------------------------------------------------- 55 static INT16 rearranged_cols[ROW]={ 0}; 56 //------------------------------------------------------------------- 57 58 double SNR_db,SNR,dbnum;//SNR:信噪比 59 int main() 60 { 61 bool s[times*(COL-ROW)],input[times*COL],output[times*(COL-ROW)]; 62 bool stage_s[COL-ROW],stage_input[COL],stage_output[COL-ROW]; 63 INT16 tx_waveform[times*COL],i,j,loop; 64 int error_num=0; 65 double rx_waveform[times*COL]={ 0},stage_rx[COL]={ 0}; 66 srand( (unsigned)time( NULL ) );//用即时的系统时间来做随机数种子.生成随机数 67 clock_t start,period; 68 69 INT16 amp;//振幅 70 double error_p;//误码率 71 //double SNR_db,SNR,dbnum;//SNR:信噪比 72 73 //配置信道信息 74 amp=1; 75 //循环多次提高平均性 76 start=clock(); 77 //产生H矩阵 78 genH(ROW,COL); 79 period=clock(); 80 printf("产生H矩阵用时%d\n",clock()); 81 //showH(); 82 83 for (dbnum=0;dbnum<=4;dbnum+=0.1) 84 { 85 SNR_db=dbnum; 86 SNR=pow(10.0,(SNR_db/10)); 87 for (int loop=0;loop max_ones_per_row)237 {238 flag=0;//有一行中的1大于最大允许239 j=rand()%COL;240 if (H[i][j]==1)//随机选择该行上某一为1的列,将该列该行上的这个1分散到其他行241 {242 newrow=rand()%ROW;//随机查找新的行243 k=0;244 while ((row_flag[newrow]>=max_ones_per_row||H[newrow][j]==1)&&k =5)//超过5次后搜索范围扩大到所有0和3318 {319 while (H[newrow][col_rearranged]==1)320 {321 newrow=rand()%ROW;322 }323 }324 H[newrow][col_rearranged]=1;325 }//endof if common==2326 /*****************************************************************/ 327 }//endof for j=0:ones_count-1328 }//endof if r!=i329 }//遍历其他行330 }//endof for i=0:ROW-1331 332 if (ckfinish==1)//如果本次循环已经不存在四环,则循环结束333 {334 printf("breakloop=%d\n",k);335 break;336 }337 }//end of loop338 339 //将所有的3变为0340 for (i=0;i
=COL)//如果找不到可以交换的列,则视为非法H矩阵404 {405 printf("非法H矩阵!!!\n");406 }407 //如果不是当前列则进行列交换408 if (j!=r)409 {410 rearranged_cols[r]=j;411 for (i=0;i
=ROW)429 {430 printf("高斯消元错误\n");431 }432 if (mid_H[i][r]==1)433 {434 for (j=r;j
=0;i--)525 {526 if (rearranged_cols[i]!=0)527 {528 temp=input[i];529 input[i]=input[rearranged_cols[i]];530 input[rearranged_cols[i]]=temp;531 }532 533 }534 }535 536 //537 //用BPSK进行调制538 void bpsk(bool input[],INT16 amp,INT16 tx_waveform[])539 {540 INT16 i;541 for (i=0;i 0.5)716 mid_out[j]=1;717 else718 mid_out[j]=0;719 }720 }//endof 纵向721 722 //如果判决条件满足,则停止译码723 724 725 for (i=0;i 0)771 return 1;772 if (x<0)773 return -1;774 if (x==0)775 return 0;776 }