473,659 Members | 2,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::queue::emp ty() 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 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
Jun 22 '18 #1
4 2310
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
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.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.
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
2213
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(){} show('delete(f):',delete(f)) ; // false g = function(){} ;
11
2178
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 execute, while the function itself returns true. A var_dump shows that all the variables are valid, and I can run the exact same update query at the mysql command line without errors. I've
4
3009
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 == null ) { return( false ) ;
1
3771
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 minIOPort); bool flag = ThreadPool.SetMinThreads(4, minIOPort); ThreadPool.GetAvailableThreads(out newWorker, out newIOPort); After running this flag is true, but the worker value is still set to 25. I am using .Net 1.1.4322 and this call was...
59
4577
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 So I changed my test for the obvious "if type(v) is bool", but I still find it confusing that "0 in " returns True
12
2663
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 strVar "Bob is from Canada" -- returns false
8
5865
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 checked to make sure that the variables have values and that the proper expire time is set and etc. Any ideas? <?php foreach($_POST as $name => $value){ if($name == 'hItem'){ $item = $value;
3
9012
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 = null;
1
1705
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 the values. If it is returns false it should not post back. Please do help me on this.
0
8751
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8539
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8630
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7360
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4176
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4342
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2759
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 we have to send another system

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.