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.
Looking at your output it's quite possible the Datasample function does something funny with the memory.
The first problem I see is that you're passing an integer as the third argument of the function and a pointer is expected. Which leads me to believe you're compiling without warnings turned on. Which is the real problem.
In light of recent comments it turns out d is actually a pointer. However, I stand by my opinion that something in that function writes over k.
Since gcc on Linux is being used you could try valgrind to quickly pinpoint the problem. It should warn you about illegal accesses to memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With