Global counter in c is not working as expected

I have a queue code that I was working on. I was trying to use a global int to keep track of the queue size.

#define MAX 100

int size=0;
int gEnqueue=gDequeue=0;

int enqueue()
{
     gEnqueue++;
     if( size == MAX )
         return QUEUE_FULL;
/* snip the actual queue handling */
     size++;
     return 0;
}

int dequeue()
{
     gDequeue++;
     if(!size)
         return QUEUE_EMPTY;

/* snip actual queue handling */
     if(size)
         size--;
     return 0;
}

      

of course a lot more code than this, but too much to post.

What's going on is the size is stuck on a mac I installed. Both functions are called an even number of times. If I flush the queue, I see that there are only 3 items in it.

What could be causing this problem?

edit # 1: made the example code match what I actually coded

This is not a thread.

edit # 2: I'm an idiot and should have done this instead of assuming. I was wrong about the calls happening even before the queue () and dequeue ().

Note to yourself, use real metrics without guessing.

+2


source to share


3 answers


If you can't use the debugger, I would suggest adding print assertions inside both functions, showing what size is, and then examines the output after the program starts. Usually, when looking at the print log, the problem is obvious.



+2


source


The simplest solution is not to call "enqueue" if size == MAX.

But if this is not possible, try this:



int size=0;
int overflow=0;

int enqueue()
{
     if( size < MAX )
         size++;
     else
         overflow++;
     return 0;
}

int dequeue()
{
     if(overflow)
         overflow--;
     else if(size)
         size--;
     return 0;
}

      

0


source


There is nothing obvious about the code you posted, so it suggests that there is something wrong with the code you cut off or the way you call the code. You will have to debug this for yourself. For now, there are two main debugging methods that will help you:

As @KPexEA suggested, debugging using printf () or other logging statements. Place printf () at the beginning and end of both functions, printing out as many states as you think might be helpful.

int enqueue()
{
     printf("enqueue(): Enter: size=%d\n", size);
     if( size == MAX ) {
         printf("enqueue(): Exit: QUEUE_FULL\n");
         return QUEUE_FULL;
     }
     /* snip the actual queue handling */
     size++;
     printf("enqueue(): Exit: size=%d\n", size);
     return 0;
}

int dequeue()
{
     printf("dequeue(): Enter: size=%d\n", size);
     if(!size) {
     printf("dequeue(): QUEUE_EMPTY\n");
         return QUEUE_EMPTY;
     }

     /* snip actual queue handling */
     if(size)
         size--;
     printf("dequeue(): Exit: size=%d\n", size);
     return 0;
}

      

By examining the output, it should be obvious what is happening with the size of your queue. (You can also count the actual number of items in your queue and print this as you enter and exit your functions.)

Another method is interactive debugging. This is especially useful for determining exactly how your code is working, but every time you run your program, you have to sit there to see how it works. (If your error occurs every time, it's easy, if it happens every time and for a while, it's hard for you to go back and recreate the program flow after the fact.) Set a breakpoint at the beginning of each of your functions and use a debugger to display the value size

. Set a different breakpoint at the end of each function, and make sure (1) actually hits the breakpoint, and (2) your expectations are met for any changes made to size

.

0


source







All Articles