1

while browsing through a queue examples, I came across

http://login2win.blogspot.in/2008/07/c-queues.html

Its a queue example in which the indexes start from -1, but the program seems to be working fine

Is this valid, shouldn't this be a undefined behavior in some cases?

4
  • Which one of the 3 examples did you use? Arrays, linked list or stl? Commented Aug 2, 2012 at 13:57
  • 2
    Oh. Yes, it's UB indeed. Commented Aug 2, 2012 at 13:59
  • 1
    Another line is rear = ++rear % MAX_SIZE; which is also UB. Please don't use that code at all! Commented Aug 2, 2012 at 14:01
  • 1
    I'd advise finding a different tutorial. You apparently already know more than the author... Commented Aug 2, 2012 at 14:21

1 Answer 1

2

You're correct. This code is invalid because it is dereferencing memory outside of that array.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.