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.
BTW is this the expected output for 0_patterns ?