Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7039187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:46:00+00:00 2026-05-28T01:46:00+00:00

a function inside my loop is somehow changing the value that i am iterating

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:46:01+00:00Added an answer on May 28, 2026 at 1:46 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a function inside of a class that returns a string. Inside this
I have this recursion loop where inside the function I have atleast 2 ajax
This is my for loop function inside the servlet Here i have a question
I have the following loop inside an function init(); which is executed onload, I
I have a function inside a class that returns a reference to a member
I have a $.post function inside of a loop. All it does it call
I'm running a for loop inside a function which is creating instances of a
I call ajax jquery function inside .each loop, and I want to do an
Quick question - I've got a $.each loop, with a function call inside with
I need to have a for loop inside my jQuery. Example: for(i=0;i<counter;i++) { $("div"+i+"").click(function(){//some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.