473,413 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

-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 4593
* 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*******@rocketmail.comwrote in message
news:CE***************@newsfe03.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<unsigned int>( -1 )
Man, thsi is really a gotcha.
Dec 4 '06 #3
On Dec 4, 12:36 pm, "Jim Langston" <tazmas...@rocketmail.comwrote:
"Jim Langston" <tazmas...@rocketmail.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*******@rocketmail.comwrote in message
news:CE***************@newsfe03.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<unsigned 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*******@rocketmail.comwrote in message
news:CE***************@newsfe03.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<unsigned 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
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...
18
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...
9
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...
49
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
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...
3
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...
4
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...
4
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...
1
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...
3
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 =...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
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
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
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...
0
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...

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.