tags:

views:

163

answers:

3

I have a bit of queue code that I was working on. I was trying to use a global int to keep track of the queue's 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;
}

there is of course much more code then that, but too much to post.

What is happening is the size gets stuck at the max I have set. Both functions get called an even number of times. If I dump the queue I can see that there are only 3 items in it.

What would cause this problem?

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

This is not threaded.

edit #2: I am an idiot and should have done this instead of assuming. I was wrong about the calls being even to the enqueue() and dequeue().

Note to self, use real metrics not guesses.

A: 

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

But if that's 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;
}
knizz
+2  A: 

If you can't use a debugger I would suggest adding print statements inside both functions showing what size equals and then after running the program examine the output. Usually when looking at the print log the problem is pretty obvious.

KPexEA
A: 

There's nothing obviously wrong with the code you posted, so this suggests there's something wrong with the code you snipped, or in the way you're calling the code. You'll have to debug this for yourself. There are two main debugging techniques that would help you at this point:

As @KPexEA suggested, debugging using printf() or other logging statements. Put a printf() at the beginning and end of both functions, printing out as much state as you think might possibly be useful.

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 become apparent what's happening with the size of your queue. (You could also count the actual number of elements in your queue and print that when you enter and exit your functions.)

The other technique is interactive debugging. This is especially useful to determine exactly how your code is flowing, but you have to sit there every time you run your program to watch how it's running. (If your bug occurs every time, that's easy; if it occurs every once and a while, it's hard to go back and recreate your program's flow after the fact.) Set a breakpoint at the beginning of each of your functions and use the debugger to display the value size. Set another breakpoint at the end of each function and make sure (1) the breakpoint actually gets hit, and (2) your expectations of any changes made to size are met.

Commodore Jaeger