Strange Bug: loop counter not incrementing in for loop

Asked by D.V.S. Phanindra

Hi, recently I got stuck up with possibly the strangest bug while programming in C in Ubuntu. The pseudo-code is as follows:

main(){
  int a;
  .
  .
  .
  fun1(a);
  .
  .
}

void fun1(int a){
  int y;
  .
  .
  //Creates double linked lists and calls several other functions (that are working fine)
  .
  .
  //Several lines below, I am calling this function
  x=fun2(y);
  .
  .
}

int fun2(int y){
  int i;
  .
  .
  for(i=0; i<2;i++){
     if(y==0){
         //Do something
     }
  .
  .
  }
  .
  .
}

   So the problem is: The variable 'i' in 'fun2' is not incrementing, As a result, the program is getting stuck in an infinite loop in this for loop in fun2.

  Somebody said that these are the issues that are dependent on the linux distribution and the compiler I am using. I did not try this program on any other distribution (I will do it soon). I am using gcc compiler.

  Can anybody help me in this issue? Thanks in advance.

Question information

Language:
English Edit question
Status:
Answered
For:
Ubuntu gcc-defaults Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Benoit Malet (benoit-malet) said :
#1

Hello !

I never experienced such issues with GCC ... Are you sure that your code doesn't touch the i in the "//Do something" part ?

Regards,
Benoît

Revision history for this message
Alan (mrintegrity) said :
#2

try pasting the real code that you have issue with.

Revision history for this message
D.V.S. Phanindra (dvsphanindra) said :
#3

Ok. I will paste here the functions fun1 and fun2 that are causing the problem. The other functions are working fine. I am trying to code the Viterbi decoder (trying to implement Convolutional encoder-decoder). These are as follows:

#include <stdio.h>
#include <stdlib.h>

//n=no of outputs; K=register size; k=input bits;
const int n=2, K=3;
struct node{
 struct node *prev0;
 struct node *prev1;
 int state[2], weight;
 struct node *next0;
 struct node *next1;
};

typedef struct node NODE;

NODE *head, *tail, *current;

// Left =next0; Right= next1;

NODE* leftnext(NODE *now);
NODE* rightnext(NODE*now);

void decoder(int input[]);

void go_prev(NODE*);

void printleft(void);
void printright(void);

void gonextstate(int);
void getnextstate(int currstate[], int input);

int hammingDist(int a1[], int a2[]); //function causing trouble

//int regstate[ ]={0,0}
int g1[ ]={1,1,1}, g2[ ]={1,0,1},output[ ]={0,0};

main(){
 int i, inputlength=9, input[ ]={1,1};

 decoder(input);//Calling decoder here.
 printf("\n");
}
void decoder(int input[]){
 int i,j, count;

 NODE *currentT[4], *prevT[4], *nextT[4], *current;

 for(i=0;i<4;i++){
  currentT[i]=prevT[i]=nextT[i]=NULL;
 }

 head=(NODE *) malloc(sizeof(NODE));
 head->prev0=NULL;
 head->prev1=NULL;
 head->state[0]=2;
 head->state[1]=0;
 head->weight=-999;
 head->next0=NULL;
 head->next1=NULL;
 //current=tail=head;
 currentT[0]=head;

 count=0;
 for(i=0; i<4; i++){
  if(currentT[i] !=NULL){
   // for left node
   currentT[i]->next0=leftnext(currentT[i]);
   printf("Weight: %d", currentT[i]->next0->weight);
   if(count!=0){
    for(j=0; j<=count; j++){
     if(hammingDist((currentT[i])->next0->state, nextT[j]->state)==0)//Calling the hamming distance here.
      break;
    }
    currentT[i]->next0=nextT[j];
   }
   else
    nextT[count]=current->next0;

   // for right node
   currentT[i]->next1=rightnext(currentT[i]);
   if(count!=0){
    for(j=0; j<=count;j++){
     if(hammingDist(currentT[i]->next1->state, nextT[j]->state)==0)//Calling Hamming distance here.
      break;
    }
    currentT[i]->next1=nextT[j];
   }
   else
    nextT[count]=current->next1;
  }
 }

 printf("count: %d\n", count);
 //go_prev(current);
 go_prev(nextT[0]);

 printf("\n");
}

int hammingDist(int a1[], int a2[]){
 int i, sum[1];
 for(i=0; i<2; i++){ //Here this 'i' is giving trouble
  if (((a1[i]) == 0 && (a2[i] == 1)) || ((a1[i] == 1) && (a2[i] == 0)))
   sum[i] = 1;
  else
   sum[i] = 0;

  //printf("hammingDist-i: %d ",p);
 }

 return sum[0] + sum[1];
}

  We tried replacing the 'for' loop with 'while'. But we could not get rid of the problem.
  Is this information sufficient? I am ready to provide the entire code but it is under construction. So there will be other errors.

Revision history for this message
Alan (mrintegrity) said :
#4

Sorry, I only know Java and this is a little over my head.

Can you help with this problem?

Provide an answer of your own, or ask D.V.S. Phanindra for more information if necessary.

To post a message you must log in.