423,822 Members | 1,356 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,822 IT Pros & Developers. It's quick & easy.

std::queue::empty() returns true but the size() is -1

P: 31
Hello,

I have a std::queue I am pushing and popping items to / from. A output threads constantly ask for the oldest buffer (on queue::front()). I wrote a safety function that checks if the queue is not empty, and another function which calls that functions, and thus, if the function returns true (queue is not empty), return front() of the queue, and if the function returns false (queue is empty) it should return a zero-constructed item. Essentially, this is my code:
Expand|Select|Wrap|Line Numbers
  1.  
  2. //Somewhere in InPort.h:
  3. std::shared_ptr<std::queue<DataBlock*>> outputData;
  4. //Beware, the queue is constructed somewhere else
  5.  
  6. bool InPort::checkIfNewBufferAvailable(){
  7.     return !outputData->empty();
  8. }
  9.  
  10. DataBlock* InPort::getNewestBuffer(){
  11.     int qSize = outputData->size();
  12.     if(checkIfNewBufferAvailable()){
  13.         return outputData->front();   //BREAKPOINT ONE
  14.     } else {
  15.         DataBlock* data = new DataBlock();   //BREAKPOINT TWO
  16.         return data;
  17.     }
  18. }
  19.  
(The variable q is just for testing)
But when I debugged that code (because of a segfault that follows because I try to access front which apparently doesn't exist), I noticed that breakpoint one is hit. That means checkIfNewBufferAvailable returns true, so the queue shouldn't be empty. But also, when debugging, I noticed that the qSize is -1, so the size of the queue is -1. Why is the queue not empty if it has a size of -1? How can such a size value even exist?
Thanks
Jun 22 '18 #1
Share this Question
Share on Google+
4 Replies


weaknessforcats
Expert Mod 5K+
P: 9,196
I'm not sure what you are looking at. std::queue.empty() returns a bool. The bool is true if the queue is empty and false otherwise.

If you are looking beneath the sheets you will find all kinda stuff but since you are not writing library code you don't care.

-1 is a convenient value to return because it is true. It's from the old days when there was no bool datatype. Function designers often used return of 0 to mean the function worked and a non-zero value that it failed. You could look up the non-zero return value and it would identify the exact error that occurred. In this case the -1 means the queue is empty.
Jun 22 '18 #2

P: 31
But the function returns true (not empty) when I can see in the debugger that it's empty. ):
Jun 22 '18 #3

weaknessforcats
Expert Mod 5K+
P: 9,196
When std::queue.empty() returns true, the queue is empty.

The function returns a bool. Either true or false.

The function InPort::checkIfNewBufferAvailable() returns
!outputData->empty(). Therefore, if the queue is empty (true) the ! operator switches the return to false.
Jun 23 '18 #4

P: 31
I know, that's what I said!
Jun 23 '18 #5

Post your reply

Sign in to post your reply or Sign up for a free account.