473,796 Members | 2,559 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

-std::map.size() reported wrong?

Expected output of this program is:
1
-1
-1
-1

Using Microsoft Visual C++ .net 2003 actual output is:
1
4294967295
4294967295
4294967295

Can someone explain why to me?

#include <string>
#include <iostream>
#include <map>

std::map<int, std::stringFoo;

int main()
{
Foo[1] = "Hello";

std::cout << Foo.size() << "\n";
std::cout << -Foo.size() << "\n";
std::cout << 0 - Foo.size() << "\n";
std::cout << 0 - (Foo.size()) << std::endl;

std::string wait;
std::getline( std::cin, wait );

} // function main
Dec 4 '06 #1
5 4622
* Jim Langston:
Expected output of this program is:
1
-1
-1
-1

Using Microsoft Visual C++ .net 2003 actual output is:
1
4294967295
4294967295
4294967295

Can someone explain why to me?
unsigned

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Dec 4 '06 #2
"Jim Langston" <ta*******@rock etmail.comwrote in message
news:CE******** *******@newsfe0 3.lga...
Expected output of this program is:
1
-1
-1
-1

Using Microsoft Visual C++ .net 2003 actual output is:
1
4294967295
4294967295
4294967295

Can someone explain why to me?

#include <string>
#include <iostream>
#include <map>

std::map<int, std::stringFoo;

int main()
{
Foo[1] = "Hello";

std::cout << Foo.size() << "\n";
std::cout << -Foo.size() << "\n";
std::cout << 0 - Foo.size() << "\n";
std::cout << 0 - (Foo.size()) << std::endl;

std::string wait;
std::getline( std::cin, wait );

} // function main
Hmm.. I was able to get the expected output this way:
#include <string>
#include <iostream>
#include <map>

std::map<int, std::stringFoo;

int main()
{
Foo[1] = "Hello";

std::cout << Foo.size() << "\n";
std::cout << -static_cast<int >( Foo.size() ) << "\n";
std::cout << 0 - static_cast<int >( Foo.size() ) << "\n";
std::cout << 0 - static_cast<int >( (Foo.size()) ) << std::endl;

std::string wait;
std::getline( std::cin, wait );

} // function main

I can understand why I have to do this, but I don't understand why I should
have to do this. It is unexpected.

The actual application was like this:

float x = (- Models.size() / 2 + ModelCount ) * 10;

I understand that size() returns an unsigned type, and since it is one, is
similar to sayign something like: static_cast<uns igned int>( -1 )
Man, thsi is really a gotcha.
Dec 4 '06 #3
On Dec 4, 12:36 pm, "Jim Langston" <tazmas...@rock etmail.comwrote :
"Jim Langston" <tazmas...@rock etmail.comwrote in messagenews:CE* **************@ newsfe03.lga...
int main()
{
Foo[1] = "Hello";

std::cout << Foo.size() << "\n";
std::cout << -static_cast<int >( Foo.size() ) << "\n";
std::cout << 0 - static_cast<int >( Foo.size() ) << "\n";
std::cout << 0 - static_cast<int >( (Foo.size()) ) << std::endl;

std::string wait;
std::getline( std::cin, wait );

} // function main

I can understand why I have to do this, but I don't understand why I should
have to do this. It is unexpected.
To get the behaviour you expec the unsigned int would have to be
converted to a signed int automagically, however the unsigned int can
contain values that the signed one can't so to do an automatic
conversion would be dangerous.

--
Erik Wikström

Dec 4 '06 #4
Jim Langston wrote:
"Jim Langston" <ta*******@rock etmail.comwrote in message
news:CE******** *******@newsfe0 3.lga...
>Expected output of this program is:
1
-1
-1
-1

Using Microsoft Visual C++ .net 2003 actual output is:
1
4294967295
4294967295
4294967295

Can someone explain why to me?

#include <string>
#include <iostream>
#include <map>

std::map<int , std::stringFoo;

int main()
{
Foo[1] = "Hello";

std::cout << Foo.size() << "\n";
std::cout << -Foo.size() << "\n";
std::cout << 0 - Foo.size() << "\n";
std::cout << 0 - (Foo.size()) << std::endl;

std::string wait;
std::getline( std::cin, wait );

} // function main

Hmm.. I was able to get the expected output this way:
#include <string>
#include <iostream>
#include <map>

std::map<int, std::stringFoo;

int main()
{
Foo[1] = "Hello";

std::cout << Foo.size() << "\n";
std::cout << -static_cast<int >( Foo.size() ) << "\n";
std::cout << 0 - static_cast<int >( Foo.size() ) << "\n";
std::cout << 0 - static_cast<int >( (Foo.size()) ) << std::endl;

std::string wait;
std::getline( std::cin, wait );

} // function main

I can understand why I have to do this, but I don't understand why I should
have to do this. It is unexpected.

The actual application was like this:

float x = (- Models.size() / 2 + ModelCount ) * 10;

I understand that size() returns an unsigned type, and since it is one, is
similar to sayign something like: static_cast<uns igned int>( -1 )
Man, thsi is really a gotcha.
I think it will get even worse on systems where size_t is 64-bit
and int is 32-bit. Then casting to an int will get you into
trouble if Models.size() is large enough (extremely large,
admittedly).

I hope I can be proven wrong :).

- J.
Dec 4 '06 #5
Jacek Dziedzic schreef:
Jim Langston wrote:
"Jim Langston" <ta*******@rock etmail.comwrote in message
news:CE******** *******@newsfe0 3.lga...
Expected output of this program is:
1
-1
-1
-1

Using Microsoft Visual C++ .net 2003 actual output is:
1
4294967295
4294967295
4294967295

Can someone explain why to me?

#include <string>
#include <iostream>
#include <map>

std::map<int, std::stringFoo;

int main()
{
Foo[1] = "Hello";

std::cout << Foo.size() << "\n";
std::cout << -Foo.size() << "\n";
std::cout << 0 - Foo.size() << "\n";
std::cout << 0 - (Foo.size()) << std::endl;

std::string wait;
std::getline( std::cin, wait );

} // function main
Hmm.. I was able to get the expected output this way:
#include <string>
#include <iostream>
#include <map>

std::map<int, std::stringFoo;

int main()
{
Foo[1] = "Hello";

std::cout << Foo.size() << "\n";
std::cout << -static_cast<int >( Foo.size() ) << "\n";
std::cout << 0 - static_cast<int >( Foo.size() ) << "\n";
std::cout << 0 - static_cast<int >( (Foo.size()) ) << std::endl;

std::string wait;
std::getline( std::cin, wait );

} // function main

I can understand why I have to do this, but I don't understand why I should
have to do this. It is unexpected.

I understand that size() returns an unsigned type, and since it is one, is
similar to sayign something like: static_cast<uns igned int>( -1 )

I think it will get even worse on systems where size_t is 64-bit
and int is 32-bit. Then casting to an int will get you into
trouble if Models.size() is large enough (extremely large,
admittedly).

I hope I can be proven wrong :).
Unlikely - you are right. Basically, if you are doing math in C++, you
must
consider the C++ math rules. Unsigned integer math is modulo UINT_MAX.
If you don't like that, use signed int, and make sure you don't have
overflows.

HTH,
Michiel Salters

Dec 5 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
3948
by: Mandus | last post by:
Hi there, inspired by a recent thread where the end of reduce/map/lambda in Python was discussed, I looked over some of my maps, and tried to convert them to list-comprehensions. This one I am not sure how to conver: Given three tuples of length n, b,i and d, I now do:
18
2549
by: Tarundeep | last post by:
hi, let us say it is a 32 bit processor then the size of the pointer is 32 bits. now i want to know what would be the size of the class with vtable pointer in it , that is it has few virtual functions. Since the very first location of the object memory points to the vtbale hence that should be 32 bit.
9
3780
by: Dr John Stockton | last post by:
Assuming default set-ups and considering all reasonable browsers, whatever that may mean, what should an author expect that his readers in general will see (with visual browsers) for a page with body like <br><br> Abc <font size=+1> Abc <font size=+1> Abc <font size=+1> Abc <font size=+1> Abc <font size=+1>
49
61192
by: Sam | last post by:
Hi all, Is there a function in the standard library that can get the size of a file? Thank you very much. Sam.
13
1005
by: Adam | last post by:
Trying to get an asp.net 2.0 app running and am receiving this error. I see a bunch of people with this error on the net, but no solution: Works fine on my local machine, deployed to a server it yields this message. Failed to map the path '/App_GlobalResources/' Stack trace: at System.Web.Hosting.HostingEnvironment.MapPathActual(VirtualPath virtualPath, Boolean permitNull)
3
2600
by: Andy Baxter | last post by:
I have an image scrolling in a viewport for a panoramic image viewer. The viewport can be resized to several set resolutions so people can adjust the size according to their bandwidth. There are hotspots on the image which have been defined using an image map. This map is generated in javascript using the following code (in the object 'tour.'): makeImageMap: function() { // creates an HTML nodeset which is an imagemap for the current...
4
1977
windows_mss
by: windows_mss | last post by:
When I Select Source & Destination Dynamically, Path Getting Scatter Across The Map... hi, i can able to get the Correct Route and Path for the corresponding Source and destination, like this, map.GetRoute('Redmond, Washington, United States','seattle, Washington, United States',VEDistanceUnit.KiloMeter,VERouteType.Shortest);
4
2033
by: =?Utf-8?B?R2F1cmF2?= | last post by:
Hello... I posted this a couple of weeks back, but could't get a reply, so trying again! I use the Setup project in C# to create a .msi file to be distributed to the clients. The size of .msi is 12 MB. This file works fine on all the target machines, but on the development machine every time I remove the existing .msi (from add/remove programs) and install a new version, the size increases by 13-14
1
1950
by: gnue | last post by:
When I install my application built using VS2008 C#, the application size is reported incorrectly. The application is actually 40 Meg in size. When you click on "Add Remove Programs" from the Control Panel my application appears in the list of installed programs but the size incorrectly is reported as 2.9 Gig. Where does Windows get the information on the application size and how do I get it to return the correct size? Thanks
3
5109
tlhintoq
by: tlhintoq | last post by:
I'm running Window7 64bit, RTM. To give this application the ability to switch between a moveable frame and no frame I am using the following methods void Fixed() { Point pBefore = this.Location; Size sBefore = this.Size; FormBorderStyle = FormBorderStyle.None; this.BackColor = Color.FromKnownColor(KnownColor.Black); this.Location = pBefore; this.Size = sBefore; }
0
9525
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10221
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
10169
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
10003
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...
1
7546
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
6785
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
5440
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
5569
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
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.