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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 51423benam
    New Member
    • Apr 2018
    • 31

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

    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:
    Code:
    //Somewhere in InPort.h:
    std::shared_ptr<std::queue<DataBlock*>> outputData;
    //Beware, the queue is constructed somewhere else
    
    bool InPort::checkIfNewBufferAvailable(){
        return !outputData->empty();
    }
    
    DataBlock* InPort::getNewestBuffer(){
        int qSize = outputData->size();
        if(checkIfNewBufferAvailable()){
            return outputData->front();   //BREAKPOINT ONE
        } else {
            DataBlock* data = new DataBlock();   //BREAKPOINT TWO
            return data;
        }
    }
    (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 checkIfNewBuffe rAvailable 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
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I'm not sure what you are looking at. std::queue.empt y() 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.

    Comment

    • 51423benam
      New Member
      • Apr 2018
      • 31

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

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        When std::queue.empt y() returns true, the queue is empty.

        The function returns a bool. Either true or false.

        The function InPort::checkIf NewBufferAvaila ble() returns
        !outputData->empty(). Therefore, if the queue is empty (true) the ! operator switches the return to false.

        Comment

        • 51423benam
          New Member
          • Apr 2018
          • 31

          #5
          I know, that's what I said!

          Comment

          Working...