473,323 Members | 1,537 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Circular Queue data structure ..

#include <stdio.h>
#define MAX 10

int queue[MAX];
int front, rear;

void init_queue()
{
front=rear=0;
}

void clear_queue(void)
{
front=rear;
}

int put(int k)
{
if ( (rear + 1) % MAX == front)
{
printf("\n Queue overflow.");
return -1;
}
queue[rear] = k;
rear = ++rear % MAX; <------------------------ 1
return k;
}

int get()
{
int i;
if (front == rear)
{
printf("\n Queue underflow.");
return -1;
}
i = queue[front];
front = ++front % MAX; <------------------------------- 2
return i;
}

void print_queue(void)
{
int i;
printf("\n Queue contents : Front -------> Rear \n");
for (i=front;i != rear ;i = ++i % MAX )
printf("%-6d", queue[i]);

the above is for implementing 'Circular Queue' data structure using
array.

when i complile it. 1 & 2 line shows the following warning message .

operation on `rear' may be undefined.
operation on `front' may be undefined.

but executues alright.. Did i miss something ?
Nov 13 '05 #1
6 37399
Hi,
"herrcho" <he*********@kornet.net> wrote in message
news:bl**********@news1.kornet.net...
| rear = ++rear % MAX;
....
| front = ++front % MAX;
.....
| when i complile it. 1 & 2 line shows the following warning message .
|
| operation on `rear' may be undefined.
| operation on `front' may be undefined.
|
| but executues alright.. Did i miss something ?

The compiler is right, and warns you about undefined behavior.
In the code lines above, the value of the same variable is
changed twice within the same statement:
var = ++var % MAX;
If var is initially 6, and MAX is 6:
- ++var will attempt to assign the value '6' to var
- the = operator will attempt to assign the value 0 to var
Which one occurs first is not defined, and the two
assignments may interfere in unexpected ways.

What you need to write is:
++var; var %= MAX; // two statements
or:
var = (var+1)%MAX; // var+1 does not try to modify 'var'

hth,
Ivan
--
http://ivan.vecerina.com
Nov 13 '05 #2
j

"herrcho" <he*********@kornet.net> wrote in message
news:bl**********@news1.kornet.net...
#include <stdio.h>
#define MAX 10

int queue[MAX];
int front, rear;

void init_queue()
{
front=rear=0;
}

void clear_queue(void)
{
front=rear;
}

int put(int k)
{
if ( (rear + 1) % MAX == front)
{
printf("\n Queue overflow.");
return -1;
}
queue[rear] = k;
rear = ++rear % MAX; <------------------------ 1
return k;
}

int get()
{
int i;
if (front == rear)
{
printf("\n Queue underflow.");
return -1;
}
i = queue[front];
front = ++front % MAX; <------------------------------- 2
return i;
}

void print_queue(void)
{
int i;
printf("\n Queue contents : Front -------> Rear \n");
for (i=front;i != rear ;i = ++i % MAX )
printf("%-6d", queue[i]);

the above is for implementing 'Circular Queue' data structure using
array.

when i complile it. 1 & 2 line shows the following warning message .

operation on `rear' may be undefined.
operation on `front' may be undefined.

but executues alright.. Did i miss something ?


Yes you did. The standard states that
``Between the previous and next sequence point an object shall have its
stored value
modified at most once by the evaluation of an expression.''

rear = ++rear;

++, operates on an lvalue, increments the value in the object -- by one --
which rear represents
Now its stored value has been modified already, then you modify it again by
assigning
this value to the object which rear designates, which violates this sequence
point law
and thus the reason for the diagnostic which your compiler so kindly gave
you.

Likewise for front.
Nov 13 '05 #3
"herrcho" <he*********@kornet.net> wrote:
rear = ++rear % MAX; <------------------------ 1 front = ++front % MAX; <------------------------------- 2 when i complile it. 1 & 2 line shows the following warning message .

operation on `rear' may be undefined.
operation on `front' may be undefined.
That's right. You modify rear and front twice between two sequence
points. That's undefined behaviour.
In these cases, the ++ operator increases rear (resp. front), and the =
operator assigns something to rear/front again. You don't actually mean
that. You don't care for the increase-object side-effect of ++; all you
need is the value-plus-one evaluation. So, instead, use an expression
that only gives you the _value_ rear+1, mod MAX.
but executues alright.


By accident. UB is allowed to do what you think it will do. It's also
allowed to do this, _until_ you demonstrate the program to your
supervisor.

Richard
Nov 13 '05 #4

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:3f***************@news.nl.net...
<snip> (working)
By accident. UB is allowed to do what you think it will do. It's also
allowed to do this, _until_ you demonstrate the program to your
supervisor.


LOL. I think this is the best description of UB I've yet seen. Thanks.
Nov 13 '05 #5
"BruceS" <no****@nospam.net> wrote:
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:3f***************@news.nl.net...
<snip> (working)
By accident. UB is allowed to do what you think it will do. It's also
allowed to do this, _until_ you demonstrate the program to your
supervisor.


LOL. I think this is the best description of UB I've yet seen. Thanks.


Not only that, but it's more or less realistic. Some forms of UB depend
on the system running the program - move it to your supervisor's
differently configured system to demonstrate it, and you might well find
that on this system, the effect of the UB is not to silently continue
but to crash or print garbage.
One particularly common form of this is the difference you get when you
always run your program in a debugging environment, but your supervisor
runs it directly from the OS.

Richard
Nov 13 '05 #6

"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:3f****************@news.nl.net...
"BruceS" <no****@nospam.net> wrote:
"Richard Bos" <rl*@hoekstra-uitgeverij.nl> wrote in message
news:3f***************@news.nl.net...
<snip> (working)
By accident. UB is allowed to do what you think it will do. It's also
allowed to do this, _until_ you demonstrate the program to your
supervisor.


LOL. I think this is the best description of UB I've yet seen. Thanks.


Not only that, but it's more or less realistic. Some forms of UB depend
on the system running the program - move it to your supervisor's
differently configured system to demonstrate it, and you might well find
that on this system, the effect of the UB is not to silently continue
but to crash or print garbage.
One particularly common form of this is the difference you get when you
always run your program in a debugging environment, but your supervisor
runs it directly from the OS.


Exactly, and why I like it. Also, while some may laugh at the idea of nasal
demons, and keep using the code because "it works fine", they're more likely
to pay attention to a warning such as above. A cohort at my first C job
liked to use the phrase "working by accident", and demanded such things be
fixed, as we never knew what change might prevent the accident.
Nov 13 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Zhaozhi Gao | last post by:
I'm doing a simple project for school. This is the first time I've ever used Access. Is there a way i can simulate a Circular Queue data structure for the datas in a table. I tried assign index...
2
by: Bob Jenkins | last post by:
I have this cute data structure that I just found a second opportunity to use. I googled for references to it, but came up empty. I probably just don't know its name. The algorithm on this...
6
by: T Koster | last post by:
After a few years of programming C, I had come to believe that I finally knew how to correctly organise my structure definitions in header files for mutually dependent structures, but I find myself...
2
by: William Stacey | last post by:
Working with implementing a circular buffer for a producer/consumer deal. Have not done one in a while. The following seems to work. However, I remember and have seen other implementation that...
10
by: avsrk | last post by:
Hi Folks I want to create a circular queue for high speed data acquistion on QNX with GNU C++ . Does any one know of efficient ways either Using STL or in any other way . I am thing of ...
7
by: toton | last post by:
Hi, I want a circular buffer or queue like container (queue with array implementation). Moreover I want random access over the elements. And addition at tail and remove from head need to be low...
2
by: marco.furlan | last post by:
Hi there, I have to write an optimized circular buffer to log events in C++. The elements are of type std::string. This should run under Linux on an ARM embedded system. I can dedicate to this...
5
by: shanknbake | last post by:
Here is my code. I've noted where the program crashes. I'm doing this program as a project for school. //cqueue.h file //HEADER FILE http://rafb.net/paste/results/Nh0aLB77.html...
2
by: lavender | last post by:
When define a maxQueue is 10, means it able to store 10 items in circular queue,but when I key in the 10 items, it show "Queue Full" in items number 10. Where is the wrong in my code? Why it cannot...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.