GCon
GCon

Reputation: 1527

Variable value changes after function call, without being used

I have been debugging the following code for the past few hours. I realize it is probably something stupid, but can't seem to figure it out.

In the second for loop (see debug printfs) the value of bitsA changes. I can't figure out why.

EDIT: In case anyone was wondering what the code in sat_count_update is:

counter->taken = (bool)((1 << counter->cbits - 1) & counter->count) ;

File: SaturatingCounter.H

#ifndef SATURATINGCOUNTER_H
#define SATURATINGCOUNTER_H
#include <stdbool.h>

//Macro function to return taken/not taken of counter
#define sat_count_taken(COUNTER) COUNTER->taken

typedef struct sat_count{
        char count;
        char cbits;
        bool taken;
} sat_count_t;

//Initialize and return counter
void sat_count_init(char bits, sat_count_t *counter){

        counter->count = 0;
        counter->cbits = bits;
        counter->taken = false;

}

//Update taken member of sat_counter_t based on count
void sat_count_update(sat_count_t *counter){
//COMMENTING OUT THIS LINE MAKES IT WORK. NORMALLY THERE IS CODE TO SET THE CORRECT VALUE        
counter->taken = true;
}       

//Up counter, respecting saturation
void sat_count_up(sat_count_t *counter){
        //If counter is saturated
        if ((counter->count < ( 1 << counter->cbits) - 1)) counter->count = counter->count + 1;
        sat_count_update(counter);
}

//Down counter, respecting saturation
void sat_count_down(sat_count_t *counter){
        //If counter is 0
        if (counter->count > 0) --counter;
        sat_count_update(counter);
}

#endif  

SaturatingCounterTest.H

#include "SaturatingCounter.H"
#include "SaturatingCounter.H" //Multiple include to test include guards
#include <stdbool.h>
#include <stdio.h>

void main(){

        //Initialize counter    
        char i,NULL1, NULL2, bitsA;
        sat_count_t mycounter;

        //Test all bit counters
        for(bitsA=1;bitsA<=5;++bitsA){

                sat_count_init(bitsA,&mycounter);
                printf("***************************** %d bits **************\n",bitsA);
                printf("**UP**\n");
                for(i=0;i<((1<<bitsA) + 1);i++) {
                        printf("Counter is currently %d, %sTAKEN.\n",mycounter.count,(!mycounter.taken) ? "NOT " : "");
                        sat_count_up(&mycounter);
                }


                printf("**DOWN**\n");
                for(i=0; i<(((1<<bitsA) + 1));i++) {
                        printf("Counter is currently %d, %sTAKEN.\n",mycounter.count,(!mycounter.taken) ? "NOT " : "");
                        //THIS IS WHERE bitsA CHANGES!
                        printf("DEBUG BEFORE: BITS: %d\n",bitsA);
//                      printf ("%p bitsA\n %p mycounter\n",&bitsA, &mycounter);
                        sat_count_down(&mycounter);
                        printf("DEBUG AFTER: BITS: %d\n",bitsA);
//                      printf ("%p bitsA\n %p mycounter\n",&bitsA, &mycounter);

                }

        }
}     

Upvotes: 2

Views: 1545

Answers (1)

Floris
Floris

Reputation: 46375

Your problem is in the line

if (counter->count > 0) --counter;

You are changing where counter is pointing - and the previous memory location is where you store bitsA :

   char i,NULL1, NULL2, bitsA;
   sat_count_t mycounter;

I suppose you meant to decrement something else - maybe

if(count->count > 0) --(counter->count)

Upvotes: 4

Related Questions