函数调用后变量值更改,未使用

时间:2013-12-16 01:01:39

标签: c boolean overflow

过去几个小时我一直在调试以下代码。我意识到它可能是愚蠢的,但似乎无法弄明白。

在第二个for循环中(参见debug printfs),bitsA的值发生了变化。我无法弄清楚原因。

编辑:如果有人想知道sat_count_update中的代码是什么:

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

档案: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);

                }

        }
}     

1 个答案:

答案 0 :(得分:4)

你的问题在

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

您正在更改counter指向的位置 - 以前的内存位置是您存储bitsA的位置:

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

我想你的意思是减少别的东西 - 也许

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