xtaci / algorithms

Algorithms & Data structures in C++.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Initializing unsigned value with a negative number.

mandliya opened this issue · comments

Hello,
In "queue.h"
We use uint32_t for m_front and m_rear and other member variables.
However in constructor, we are assigning m_rear as -1.
I understand the purpose of assigning it -1, as we want to enqueue first element at m_rear = 0 and we increment m_rear with each enqueue operation.
Although, the code behaves right, as assigning -1 would overflow it to UMAX and then adding 1 to it will bring it back to 0. I just wanted to understand the purpose of declaring it uint32_t instead of simple integer.

Your repository is really awesome, learning a lot from it.

Thanks

uint32_t bit guarantees 32 bit unsigned and so queue can accommodate more elements