473,507 Members | 8,335 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

31 New Member
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
4 2300
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
51423benam
31 New Member
But the function returns true (not empty) when I can see in the debugger that it's empty. ):
Jun 22 '18 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
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
51423benam
31 New Member
I know, that's what I said!
Jun 23 '18 #5

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

Similar topics

2
2200
by: vakap | last post by:
function show() { var s = '' ; for (var i = 0; i<arguments.length; s += '\n'+arguments) ; typeof(window) != 'undefined' ? window.alert(s) : WScript.Echo(s) ; } function f(){}...
11
2159
by: John Moore | last post by:
Hi, I must be missing something obvious. I cannot get this function to run the update query on Line 6, although the call to run the query evaluates true, and both update_cat_total functions...
4
3004
by: Sly | last post by:
Hi! I am facing an unbreakable wall. I have a class 'x' with one array in it called arr; in Equals(x arg) routine I compare the elements of the array. But first I check whether if( arg.arr ==...
1
3749
by: Tim Begin | last post by:
I am attempting to use the ThreadPool.SetMinThreads method as shown in the MSDN example code: int minWorker, minIOPort; int newWorker,newIOPort; ThreadPool.GetAvailableThreads(out minWorker, out...
59
4532
by: Pierre Quentel | last post by:
Hi all, In some program I was testing if a variable was a boolean, with this test : if v in My script didn't work in some cases and I eventually found that for v = 0 the test returned True ...
12
2650
by: ross.oneill | last post by:
Hi, Is there any function in php that will match a word exactly and if it finds it, it returns true. For example if I search for "CA" strVar = "Bob is from Los Angeles CA" - return true ...
8
5848
by: SupraFast | last post by:
I have two hosting accounts. On one, my setcookie script works fine; cookies are created. On the other, the same script doesn't work. The function returns TRUE, but no cookies is created. I...
3
8992
by: jwdvorak | last post by:
In the following method: public boolean isMemberAlive(String userSsn) throws SQLException { boolean isMemberAlive = false; Connection c = null; Statement s = null; ResultSet rs =...
1
1674
by: muthukumaran08 | last post by:
I have textboxes and dropdownlist on my page. And i have image button when i click this image button i m validating textboxes on javascript and if it returns true, i need to do a post back to save...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7029
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7481
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3190
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1537
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.