Why is sizeof(unsigned double) equal to 4?

C++Visual Studio-2010

C++ Problem Overview


A colleague of mine asked if there is unsigned double, and I said there isn't, but I still checked it, and this compiles in Microsoft Visual C++ 2010:

unsigned double a;
double b;
printf("size_a=%d size_b=%d", (int) sizeof(a), (int) sizeof(b));

It outputs size_a=4 size_b=8. That is, four bytes for unsigned double, and eight bytes for double.

C++ Solutions


Solution 1 - C++

unsigned double is invalid. This is also true in MSVC. When compiling the above code in MSCV 2010 with warnings enabled you get:

warning C4076: 'unsigned' : can not be used with type 'double'

The compiler actually ignores double after unsigned, making your a actually an unsigned int.

If you try the following:

unsigned double a = 1.0;

You actually get two warnings:

warning C4076: 'unsigned' : can not be used with type 'double'
warning C4244: 'initializing' : conversion from 'double' to 'unsigned int', possible loss of data

Interestingly, there is no C4076 warning in MSDN for VS2010. It is present only for VS2005 and VS2008.

Solution 2 - C++

If you set the warning level higher (/W3 in my test), you will get an appropriate warning:

> warning C4076: 'unsigned' : can not be used with type 'double'

If you then use the debugger to inspect the variable, all becomes clear:

enter image description here

You can see that the variable is in fact an unsigned int

Solution 3 - C++

Combining unsigned with double in the declaration specifier sequence is not valid C++. This must be an MSVC extension (or bug) of some sort.

> As a general rule, at most one type-specifier is allowed in the complete decl-specifier-seq of a declaration or in a type-specifier-seq or trailing-type-specifier-seq. The only exceptions to this rule are the following: > > - const can be combined with any type specifier except itself. > - volatile can be combined with any type specifier except itself. > - signed or unsigned can be combined with char, long, short, or int. > - short or long can be combined with int. > - long can be combined with double. > - long can be combined with long.

Solution 4 - C++

Unsigned and signed act as type qualifiers in MSVC where possible (unsigned char, signed short etc). If it's impossible to do that, such as unsigned bool, or signed double, the requested type is not created. And the type is just treated as unsigned [int] and signed [int].

Solution 5 - C++

It is a bug in VS2010. VS2012 gives the following error for that line of code.

error CS1002: ; expected

It is expecting a ';' before the keyword 'double'.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsashoalmView Question on Stackoverflow
Solution 1 - C++CygnusX1View Answer on Stackoverflow
Solution 2 - C++CliffordView Answer on Stackoverflow
Solution 3 - C++Joseph MansfieldView Answer on Stackoverflow
Solution 4 - C++Boyko PerfanovView Answer on Stackoverflow
Solution 5 - C++CramerTVView Answer on Stackoverflow