compiler - Dave's Blog

Search
My timeline on Mastodon

MSVC++ 64bit Enums

2013 Jul 1, 1:00

If you want to represent a value larger than 32bits in an enum in MSVC++ you can use C++0x style syntax to tell the compiler exactly what kind of integral type to store the enum values. Unfortunately by default an enum is always 32bits, and additionally while you can specify constants larger than 32bits for the enum values, they are silently truncated to 32bits.

For instance the following doesn't compile because Lorem::a and Lorem::b have the same value of '1':


enum Lorem {
a = 0x1,
b = 0x100000001
} val;

switch (val) {
case Lorem::a:
break;
case Lorem::b:
break;
}

Unfortunately it is not an error to have b's constant truncated, and the previous without the switch statement does compile just fine:


enum Lorem {
a = 0x1,
b = 0x100000001
} val;

But you can explicitly specify that the enum should be represented by a 64bit value and get expected compiling behavior with the following:


enum Lorem : UINT64 {
a = 0x1,
b = 0x100000001
} val;

switch (val) {
case Lorem::a:
break;
case Lorem::b:
break;
}
PermalinkComments64bit c++ development enum msvc++ technical

Sometimes the bug isn't in your code, it's in the CPU (dragonflybsd.org)

2012 Mar 7, 8:00

Fascinating, but really most of the time it is in your code.  Really you should look there first.  Usually not the compiler’s fault, or the OS’s fault, or a loose wire in the CPU…

PermalinkCommentstechnical programming cpu

ILSpy - SharpDevelop Wiki

2011 Mar 28, 4:06"ILSpy is the open-source .NET assembly browser and decompiler. Development started after Red Gate announced that the free version of .NET Reflector would cease to exist by end of February 2011."PermalinkComments.net tools reflector c# development csharp dotnet technical tool

/EP (Preprocess to stdout Without #line Directives) (C++)

2008 Jan 28, 2:42Use this option with cl.exe (the Visual Studio C/C++ compiler) to see what your files look like after all the #define macro magic occurs. Useful when debugging crufty or organic macros.PermalinkCommentsmicrosoft msdn reference c++ cpp preprocessor tool compiler cl

Wp64 Issues

2007 Aug 6, 3:43Miladin told me about the Visual Studio compiler's promising option Wp64 that finds 64bit portability issues when compiling in 32bit. If, for instance, you cast from a (long*) to a (long) you get a W4 warning. However, the #defines are still set for 32bit builds. This means that other parts of the code can make assumptions based on the #defines that are valid on 32bit but generate 64bit errors or warnings.

For instance, in winuser.h the public published Windows header file there's the following:
...
#ifdef _WIN64
...
WINUSERAPI
LONG_PTR
WINAPI
SetWindowLongPtrA(
    __in HWND hWnd,
    __in int nIndex,
    __in LONG_PTR dwNewLong);
...
#else  /* _WIN64 */
...
#define SetWindowLongPtrA   SetWindowLongA
...
#endif /* _WIN64 */
...
In 64bit everything's normal but in 32bit SetWindowLongPtrA is #defined to SetWindowLongA which takes a LONG rather than a LONG_PTR. So take the following code snippet:
...
LONG_PTR inputValue = 0;
LONG_PTR error = SetWindowLongPtrA(hWnd, nIndex, inputValue);
...
This looks fine but generates warnings with the Wp64 flag.

In 64 bit, p is cast to (LONG_PTR) and that's great because we're actually calling SetWindowLongPtrA which takes a LONG_PTR. In 32 bit, p is cast to (LONG_PTR) which is then implicitly cast to (LONG) because we're actually calling SetWindowLongA. LONG and LONG_PTR are the same size in 32bit which is fine but if you turn on the Wp64 flag there's a W4 warning because of the implicit cast from a larger size to a smaller size if you were to compile for 64bit. So even though doing a 32bit or 64bit compile would have worked just fine, if you turn on the Wp64 flag for 32bit you'd get an error here.

It looks like I'm the most recent in a list of people to notice this issue. Well I investigated this so... I'm blogging about it too!PermalinkCommentswp64 technical 64bit compiler c++ visual-studio setwindowlongptra

Fab@Home: 3D objects from your printer for under $2,500: (Ars Technica)

2007 Apr 11, 1:18Article on current 3D printer technology. Reminds me of an April fools article on Ars a while ago about nano-compilers. The future is now!PermalinkCommentsarticle fabrication printer 3d

Clocking and Stocking the NSC1 Pro: The Class 1 Nanite Compiler And You (Ars Technica)

2007 Apr 11, 12:58An old ars technica article about getting the most out of your nano-compiler (April Fools article). I remember reading this in high school.PermalinkCommentshumor nano nanocompiler fabrication article

Write a VIM Compiler Plugin

2006 Sep 19, 12:52PermalinkCommentsvim programming howto

Debugging Tools and Symbols: Getting Started

2006 Mar 9, 9:53PermalinkCommentsmicrosoft windows decompiler debug debugger x64 x86

Lutz Roeder's Programming.NET C# VB CLR WinFX

2005 Dec 5, 6:04PermalinkCommentsfree software csharp dotnet development decompiler tools windows reflector
Older Entries Creative Commons License Some rights reserved.