需要帮助找到为什么一个for循环的计数器变量被循环内的一个函数改变 [英] need help finding why a for loop's counter variable is being altered by a function inside the loop

查看:165
本文介绍了需要帮助找到为什么一个for循环的计数器变量被循环内的一个函数改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的循环内部的一个函数是以某种方式改变我正在迭代的值,我不知道如何。对不起,如果这是很不好描述。



里面这个for循环

  int k; (k = 0; k <512; k ++)
{
// Discardheader(d);

//实际上并没有做任何事情,因为它是一个header.f
int databit = Getexpecteddata(d + 4 * k + 1);
printf(%d,k);
int transmit =数据采样(& datastate,& datalength,d + 4 * k + 2,dataerr,dataloc,databit);
printf(%d,k);
Clocksample(& clockstate,& clocklength,d + 4 * k + 3,clockerr,transmit);
printf(%d \\\
,k);




$我得到这个输出

  16 16 16 
17 17 17
18 18 18
19 19 19
20 20 20
21 1 1
2 2 2
3 3 3
4 4 4

所以Datasample一旦达到21就改变k的值。d是char * d类型,表示一个缓冲区,在这里我读取一个文件。改变输入文件不会改变,在21开关发生。这里是datasample的代码:

  int Datasample(int * state,int * length,char * d,int * type, int位置,int数据)
{
int match = 1;如果数据采样和延迟tx匹配,则
if(((d [0]>> location)& 1)!= data)
{
match = 0;
if(data)
{
type [2] ++;
}
else
{
type [1] ++;
}

}

int ia; (((d [0]>> ia)& 1)== *状态下的(bia = 7; ia> = 0; ia-)

) )//找到一个边
{
* length ++;
}
else
{
int距离,偏差,devflag = 1; //边距离采样点的距离。应该是大约4
如果(位置> 3)//偏差距离那么距离是多远4
{distance = location-ia;}
else
{distance = ia - location;}

deviation = abs(4-distance); ((偏差> = devmax)&& match&&& devflag)


{
devflag = 0;
if(data)
{
type [2] ++;
}
else
{
type [1] ++;
}


* state =((d [0]> ia)& 1);
* length = 1;
}

}

return((d [0]>> location)& 1);

}

一旦击中21,回滚到1?



在此先感谢。我不知道我在做什么。

解决方案

我不知道程序应该做什么(注释并没有太大帮助,变量名称不是描述性的)。主要的问题似乎是 * lenght ++; 语句,这会使指针碰撞超出识别范围。接下来的 * length = 1; 是不好的工作。



额外的评论风格:对 unsigned 类型进行一些位操作;签名延伸可能会导致1位出现在不需要的地方。另外:建议使用无符号类型的计数器和索引;这会导致程序在下溢时严重崩溃。

$ $ $ $ $ $ $ $ $ $ $ $ int $ Datasample(int * state,int * length,char * d ,int *类型,无符号位置,int数据)
{
int match = 1; //如果数据采样和延迟tx匹配,
int devflag = 1; / *从内部循环中提升这个变量* /
unsigned bitpos; / *重命名并更改为无符号(位置以及)* /

/ *注意:移位为零(或负数)未定义* /
if(((d [0]> gt ;>位置)& 1)!= data){
match = 0;
if(data)type [2] ++;
else type [1] ++;

再次:零位移位未定义* /
(bitpos = 8; bitpos--> 0;)
{
// find (((d [0]> bitpos)& 1)== *状态)*长度+ = 1的边缘
;
else
{
int距离,偏差;
//边缘距采样点的距离。应该是大约4
//偏差距离距离4
distance =(location> 3)有多远?位置 - bitpos:bitpos - 位置;
deviation = abs(4-distance);

if(deviation> = devmax&& match&& devflag)
{
devflag = 0;
if(data)type [2] ++;
else type [1] ++;
}
* state =((d [0]>> bitpos)& 1);
* length = 1;
}

}

return((d [0]>> location)& 1);





$ b BTW是0_patterns的预期输出吗?

 文件包含5769个事件
文件包含6938个错误
文件包含543个溢出错误
文件包含6395个非填充错误
错误nonspill#spill#
类型D 2250 451
类型C 0 0
类型B 4145 92

案例1 1195 307
案例2 0 20
案例3 1055 124
案例4 0 0
案例5 0 0
案例6 0 0
案例7 0 0
案例8 1160 9
案例9 0 0
案例10a 1472 39
案例10b 1513 29
案例10c 0 15


a function inside my loop is somehow changing the value that i am iterating over, and i'm not sure how. i'm sorry if this is very poorly described.

inside this for loop

int k;

for( k = 0; k < 512; k++)
{
    // Discardheader(d);      // doesnt actually do anything, since it's a header.f
    int databit = Getexpecteddata(d+4*k+1);
    printf("%d ",k);
    int transmitted = Datasample(&datastate, &datalength, d+4*k+2,dataerr,dataloc, databit);
    printf("%d ",k);
    Clocksample(&clockstate, &clocklength, d+4*k+3,clockerr, transmitted); 
    printf("%d \n",k);

}

i get this output

16 16 16
17 17 17
18 18 18
19 19 19
20 20 20
21 1 1
2 2 2
3 3 3
4 4 4

so somehow Datasample is changing the value of k once it reaches 21. d is type char * d and represents a buffer where i read a file in. changing input files does not change that at 21 the switch happens. here is the code for datasample:

int Datasample (int* state, int* length, char *d, int *type, int location, int data)
{
    int match = 1;                                  // if data sample and delayed tx match,
    if ( ((d[0] >> location) & 1) != data)
    {
        match = 0;
        if(data)
    {
        type[2]++;  
    }
    else
    {
        type[1]++;
    }   

} 

int ia;
for( ia = 7; ia>=0; ia--)                           
{
    if ( ((d[0] >> ia) & 1) == *state)          // finds an edge
    {
        *length++;
    }
    else
    {
        int distance, deviation,devflag=1;      // distance the edge is from the sample point. should be about 4
        if ( location > 3)                      // deviation is how far the distance then is from 4
        {distance = location - ia;}
        else
        {distance = ia - location;}

        deviation = abs(4-distance);

        if( (deviation >= devmax) && match && devflag)
        {
            devflag =0;    
            if(data)
            {
                type[2]++;  
            }
            else
            {   
                type[1]++;
            }   

        }
        *state = ((d[0] >> ia) & 1);
        *length = 1;
    }

}

return ((d[0] >> location) & 1);

}

what is causing the k to roll back to 1 once it hits 21?

thanks in advance. i have no idea what i'm doing.

解决方案

I don't have the faintest idea what the program is supposed to do (comments don't help very much, the variable names are not that descriptive). The main problem appears to be the *lenght++; statement, which bumps the pointer beyond recognition. A subsequent *length = 1; does the dirty work.

Extra comment on style: it is preferable to perform bit operations on unsigned types; sign-extention could cause '1' bits to appear at unwanted places. Also: it is advisable to use unsigned types for counters and indexes; that will cause the program to crash more rigorously on underflow.

int Datasample (int *state, int *length, char *d, int *type, unsigned location, int data)
{
    int match = 1;                                  // if data sample and delayed tx match,
    int devflag = 1;        /* hoisted this variable from inner loop */
    unsigned bitpos ;     /* renamed and changed to unsigned ( location as well) */

                          /* Note: shift by zero (or negative) is undefined */
    if ( ((d[0] >> location) & 1) != data) {
        match = 0;
        if(data) type[2]++;
        else type[1]++;
    } 
                        /* Again: shift by zero is undefined */
    for( bitpos = 8; bitpos-- > 0; )                           
    {
          // find an edge
        if ( ((d[0] >> bitpos) & 1) == *state) *length += 1;
        else
        {
            int distance, deviation;
                                  // distance the edge is from the sample point. should be about 4
                                  // deviation is how far the distance then is from 4
            distance = (location > 3) ?  location - bitpos : bitpos - location;
            deviation = abs(4-distance);

            if (deviation >= devmax && match && devflag)
            {
                devflag =0;    
                if (data) type[2]++;
                else type[1]++;
            }
            *state = ((d[0] >> bitpos) & 1);
            *length = 1;
        }

    }

    return ((d[0] >> location) & 1);
}

BTW is this the expected output for 0_patterns ?

 File contains 5769 events 
 File contains 6938 errors 
 File contains 543 spill errors 
 File contains 6395 nonspill errors 
    Error       nonspill #  spill #     
    Type D      2250        451         
    Type C      0       0           
    Type B      4145        92          

    Case 1      1195        307         
    Case 2      0       20          
    Case 3      1055        124         
    Case 4      0       0           
    Case 5      0       0           
    Case 6      0       0           
    Case 7      0       0           
    Case 8      1160        9           
    Case 9      0       0           
    Case 10a    1472        39          
    Case 10b    1513        29          
    Case 10c    0       15

这篇关于需要帮助找到为什么一个for循环的计数器变量被循环内的一个函数改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆