V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
itfanr
V2EX  ›  C

C语言内存出现 -1.#IND00 是怎么回事?

  •  
  •   itfanr · 2013-08-05 16:34:47 +08:00 · 1673 次点击
    这是一个创建于 4158 天前的主题,其中的信息可能已经有所发展或是发生改变。
    情景是这样的:

    我仿真发送01数据,然后接收。当发送帧长为500时,假设发送1000次,都不出错,但是一旦帧长变成1024,就会偶然性(如果一直错误反而好了)出错。。

    其中用到了几个double型的数组,函数调用前我都用动态数组了。但是我搞不明白,为什么出错时我查看数组中的数据,内存中变成了-1.#IND00 。。。


    求大神帮忙,程序不大,如果需要我发你一份,帮我看看。感激涕零~~~~~
    第 1 条附言  ·  2013-08-06 12:33:07 +08:00
    部分代码:

    15 条回复    1970-01-01 08:00:00 +08:00
    txx
        1
    txx  
       2013-08-05 17:00:58 +08:00
    我曾经在double 溢出的时候收到了这个......
    aoyoo
        2
    aoyoo  
       2013-08-05 17:03:23 +08:00
    “我仿真发送01数据”具体指的是什么呢?com口发送?socket发送?还是什么?
    itfanr
        3
    itfanr  
    OP
       2013-08-05 17:24:22 +08:00
    @aoyoo 自己程序发送 不通过其他方式 send() receive() 函数而已 我做的通信算法
    xdeng
        4
    xdeng  
       2013-08-05 17:24:50 +08:00
    贴出来看看啊
    xdeng
        5
    xdeng  
       2013-08-05 17:25:44 +08:00
    越界? 溢出?
    timonwong
        6
    timonwong  
       2013-08-05 17:47:11 +08:00   ❤️ 1
    自己检查代码,-1.#IND就是NaN,常见于除数为0的错误
    itfanr
        7
    itfanr  
    OP
       2013-08-05 20:39:04 +08:00
    @xdeng 怎么发私信?能告诉我你邮箱吗?
    itfanr
        8
    itfanr  
    OP
       2013-08-05 20:39:47 +08:00
    @timonwong 可是为什么我500可以,1000就不行了。不可能出现除数为0的啊!
    timonwong
        9
    timonwong  
       2013-08-05 20:54:58 +08:00
    @itfanr
    那么我猜你发送、接收没有处理好,要么数据有丢失,要么数据有覆盖。你最好是在发送端Dump发送的数据(或者只Dump异常的数据),如果发送的数据也有异常,那么先暂时排除接收端的问题,检查发送端的代码(先检查浮点运算,再检查内存),浮点运算出现NaN,除了除以0,还有很多情况:取负数的平方根、0的0次方,0和正、负无穷相乘,等等等等,如果你有除法,检查被除数为0(这个为0不见得是等于0,具体要看epsilon,请查看IEEE754相关资料,快速了解的话请先看这篇: http://en.wikipedia.org/wiki/Machine_epsilon)。
    另外所以要仔细查看有没有未初始化变量,或者变量的值被覆盖了(比如覆盖全`1`)。
    itfanr
        10
    itfanr  
    OP
       2013-08-05 20:55:17 +08:00
    用到了两个主要文件,在main里面调用turbo_decode函数:
    //部分main代码:
    double ** soft_out ;
    soft_out = (double **)malloc(sizeof(double *) * inter_num);

    for (int soft_out_i =0; soft_out_i < inter_num ; soft_out_i++)
    {
    soft_out[soft_out_i] = (double *)malloc(sizeof(double)* frame_size);
    }
    turbo_decode(Lc ,g,ispunctured ,pattern ,decode_method ,receive ,soft_out) ;



    //1.decode.cpp

    #include "stdafx.h"
    #include "tools.h"
    #include <math.h>
    #include <string.h>
    #include "decode.h"
    #include "decode.h"

    void turbo_decode(double Lc ,int g[][4] ,int ispunctured ,const int pattern [] ,int decode_method ,const double * receive , double **soft_out){

    if (ispunctured)//删余
    {
    //
    }
    else
    {
    /*double e_info [frame_size+3] ;
    double die_info [frame_size+3] ;
    double so [frame_size+3 ] ;
    double so1 [frame_size+3] ;*/
    double in1 [2][frame_size+3] ;
    double in2 [2][frame_size+3] ;

    for (int i=0; i<frame_size+3; i++)
    {
    in1[0][i] = receive[3 * i] ;
    in1[1][i] = receive[3 * i + 1] ;
    in2[1][i] = receive[3 * i + 2] ;
    }

    for(int i=0 ;i< frame_size+3 ; i++){
    in2[0][i] = in1[0][pattern[i]-1];//交织 因为交织的数组为(1-1024,故减1)
    }

    double * e_info = (double *)malloc(sizeof(double) * (frame_size+3) );
    double * die_info = (double *)malloc(sizeof(double) * (frame_size+3) );
    double * so = (double *)malloc(sizeof(double) * (frame_size+3) );
    double so1 [frame_size+3] ;

    for (int i=0; i<frame_size+3; i++)
    {
    e_info[i] = 0 ;
    }

    for (int inter = 0 ; inter< inter_num; inter++)//开始迭代
    {

    // printf(" \n\n inter = %d\n\n ", inter) ;

    for(int i=0 ;i< frame_size+3 ; i++){ //解交织
    die_info [pattern[i]-1] = e_info[i] ;
    }
    // printf("before decoder 1---------die_info[0] = %lf \n", die_info[0] ) ;

    maxLogMap(in1, g, Lc, (const double *)die_info, e_info, so) ; //

    //rintf("after decoder 1---------e_info[0] = %lf \n", e_info[0] ) ;


    for(int i=0 ;i< frame_size+3 ; i++){
    die_info[i] = e_info[pattern[i]-1] ;//交织
    }

    // printf("before decoder 2---------die_info[0] = %lf \n", die_info[0] ) ;

    maxLogMap(in2 , g, Lc, (const double *) die_info, e_info, so) ; //改变了e_info

    //rintf("after decoder 2 ---------e_info[0] = %lf \n", e_info[0] ) ;

    for(int i=0 ;i< frame_size+3 ; i++){ //解交织
    so1 [(pattern[i]-1)] = so[i] ;
    }

    for (int i1 = 0 ; i1<frame_size ; i1++)//舍去最后的三个数字
    {
    soft_out[inter][i1] = so1[i1] ;
    }

    }

    free(e_info) ;
    e_info = NULL ;
    free(die_info) ;
    die_info = NULL ;
    free(so) ;
    so=NULL;
    }//else 结束
    }

    void genTrellis(int g[][4],int lstate [][8] , int nstate [][8],int lpara[][8],int npara[][8]){

    int k = 4 ;
    int m =k-1 ;
    int in = 0 ;
    int state_temp [3] ;//input0和input1都用
    int state[3] ;//input0和input1都用

    int *gp1 = &g[0][1] ;//g(1,2:end)
    int *gp2 = &g[1][0] ;//g(2,:)
    int para = 0 ;
    int state0_temp1 [4] ;
    int state0_temp2[3] ;

    int state1_temp1[4] ;
    int state1_temp2[3] ;
    int nstate_index ;

    for (int i=0 ;i<8 ;i++ )
    {
    de2bi(i,state_temp) ;

    //input 0
    fliplr(state,state_temp,3) ;
    in = (matrix_Mul(gp1,state,3)%2)^0 ;
    state0_temp1[0] = in ;
    for (int j = 1; j<4 ; j++)
    {
    state0_temp1[j] = state[j-1] ;
    }
    para = matrix_Mul(gp2,state0_temp1,4)%2 ;
    right_shift(in,state,3) ;

    fliplr(state0_temp2,state,3) ;
    nstate_index = bi2de(state0_temp2, 3) ;
    nstate[0][i] = nstate_index +1 ;
    lpara[0][nstate_index] = 2 * para -1 ;
    lstate[0][nstate_index] = i+1 ;


    //input 1
    fliplr(state,state_temp,3) ; //重置state
    in = (matrix_Mul(gp1, state,3)%2)^1 ;
    state1_temp1[0] = in ;
    for (int j = 1; j<4 ; j++)
    {
    state1_temp1[j] = state[j-1] ;
    }
    para = matrix_Mul(gp2,state1_temp1,4 )%2 ;
    right_shift(in,state,3) ;

    fliplr(state1_temp2, state, 3) ;
    nstate_index = bi2de(state1_temp2, 3);
    nstate[1][i] = nstate_index +1 ;
    lpara[1][nstate_index] = 2* para - 1 ;
    lstate[1][nstate_index] = i+1 ;
    }
    //以下产生npara

    for (int i = 0 ; i<2 ;i++)
    {
    for (int j=0; j<8; j++)
    {
    //找到上一个状态
    int state_temp ;
    state_temp = lstate[i][j] ;
    int para_temp ;
    para_temp = lpara[i][j] ;
    npara[i][state_temp-1] = para_temp ;
    }
    }

    }

    2.subdecode.cpp

    #include "stdafx.h"
    #include "tools.h"
    #include <math.h>
    #include "decode.h"

    double maxVect(const double vect[], int vect_len) ;
    void maxLogMap(const double input[][frame_size+3] , int g[][4],double Lc,const double * pri_info, double * e_info, double *so ) {
    //产生网格
    int lstate [2][8];
    int nstate [2][8] ;
    int lpara [2][8] ;
    int npara [2][8] ;

    genTrellis(g, lstate , nstate, lpara, npara) ;

    //主要译码部分
    const int nstates = 8 ;
    //alpha beta gamma 初始化
    double alpha[frame_size +3][nstates] , beta[frame_size +3+1][nstates] ;
    double gamma0[frame_size +3][nstates], gamma1[frame_size +3][nstates] ;

    const int infinity = 1000 ;
    alpha[0][0] = 0 ;
    for (int st = 1; st<nstates; st++)
    {
    alpha [0][st] = -infinity ;//初始化第1行
    }
    //beta
    double log_temp = 1.0/8 ;
    for (int beta_col = 0; beta_col<nstates; beta_col++ )
    {
    beta[frame_size +3][beta_col] = log(log_temp) ; //初始化最后一行
    }
    //------------------------------------------------------------------------------------

    for (int i = 0; i<frame_size +3; i++)
    {
    for (int st = 0; st<nstates; st++)
    {
    double mul_temp ;
    mul_temp = -1 * input[0][i] + lpara[0][st] * input[1][i] ; //输入0
    gamma0[i][st] = -1 * pri_info[i]/2 + Lc/2 * mul_temp ;

    mul_temp = 1 * input[0][i] + lpara[1][st] * input[1][i] ; //输入1
    gamma1[i][st] = pri_info[i]/2 + Lc/2 * mul_temp ;
    }

    }

    //--------------------------------------------------------------------------------------

    for (int i = 1; i<frame_size +3; i++)
    {
    int lstate_temp1 ,lstate_temp2 ;
    double data0 ,data1 ;
    for (int st = 0; st<nstates; st++ )
    {
    lstate_temp1 = lstate[0][st] -1;
    data0 = gamma0[i-1][st] + alpha[i-1][lstate_temp1] ;

    lstate_temp2 = lstate[1][st]-1 ;
    data1 = gamma1[i-1][st] + alpha[i-1][lstate_temp2] ;

    alpha[i][st] = (data0>data1 ? data0 :data1 );

    }
    }
    //------------------------------------------------------------------------------------------------------------

    for (int i = frame_size +3-1; i>0 ; i--)
    {
    int data0, data1 ;
    int nstate_temp0,nstate_temp1 ;
    for (int st = 0 ; st<nstates; st++)
    {
    nstate_temp0 = nstate[0][st] -1;
    data0 = gamma0[i][nstate_temp0] + beta[i+1][nstate_temp0] ;

    nstate_temp1 = nstate[1][st] -1;
    data1 = gamma1[i][nstate_temp1] + beta[i+1][nstate_temp1] ;

    beta[i][st] = (data0>data1 ? data0 :data1 );

    }
    }

    //----------------------------------------------------------------------------------------------------------------
    double temp0[nstates] = {0} ;
    double temp1[nstates] ={0 };
    double so_temp [frame_size+3] = {0};

    for (int i=0; i<frame_size +3; i++)
    {
    int lstate_temp1, lstate_temp2 ;
    for (int st = 0 ; st<nstates; st++)
    {
    lstate_temp1 = lstate[0][st] -1 ;
    lstate_temp2 = lstate[1][st] -1;

    temp0[st] = gamma0[i][st] + alpha[i][lstate_temp1] + beta[i+1][st] ;
    temp1[st] = gamma1[i][st] + alpha[i][lstate_temp2] + beta[i+1][st] ;
    }
    so_temp [i] = maxVect(temp1,nstates) - maxVect(temp0, nstates) ;

    }

    //----------------------------------------------------------------------------

    for (int i =0; i<frame_size+3; i++)
    {
    so[i] = so_temp[i] ;
    }

    for (int i = 0; i<frame_size +3; i++)
    {
    e_info[i] = so[i] - pri_info[i] - Lc * input[0][i] ;
    }


    }

    double maxVect(const double vect[], int vect_len){

    double max = vect[0] ;
    int i = 1 ;
    while (i<vect_len)
    {
    if(vect[i]>=max ){
    max = vect[i] ;
    }
    i++ ;
    }
    return max ;
    }
    itfanr
        11
    itfanr  
    OP
       2013-08-05 20:57:21 +08:00
    @xdeng 代码我贴了
    itfanr
        12
    itfanr  
    OP
       2013-08-05 20:57:46 +08:00
    @timonwong 代码我贴了
    detailyang
        13
    detailyang  
       2013-08-05 22:50:04 +08:00
    ...你应该帖glist上 这样看起来舒服。。
    itfanr
        14
    itfanr  
    OP
       2013-08-05 23:32:06 +08:00
    @detailyang glist是什么?我也想高亮啊 这里不行 很郁闷
    blueandhack
        15
    blueandhack  
       2013-08-06 02:13:39 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   991 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 21:41 · PVG 05:41 · LAX 13:41 · JFK 16:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.