News for hackers. Stuff that matters.
Re:Issues with porting C++ code to a 64-bit platform by mpyne on Monday April 01, 2013 @01:49PM (#5474652)
I had issue #2 (varags) bite me once on a C function where a NULL pointer was used as a sentinel.

In C++ we normally use '0' as the name of the null pointer, as C's NULL macro ((void*)0) gives compilation errors normally (though it would have worked here). And of course, who knows what the NULL macro is defined to in C++ mode, it could be plain '0' or a compiler-specific type.

With varargs '0' simply translates to int 0. That is, 32-bit int 0, whereas the function was expecting a 64-bit null pointer. So on 32-bit mode it happened to work fine, but crashed when compiled for 64-bit platforms.

Re:Issues with porting C++ code to a 64-bit platform by yew on Monday April 01, 2013 @03:11PM (#5475210)
Huh. I'd always thought C++ shared C's definition of NULL, but looking I see that it doesn't. What a weird thing to change.

At least they've added some safety with nullptr_t.

Re:Issues with porting C++ code to a 64-bit platform by plorkyeran on Monday April 01, 2013 @05:07PM (#5475937)
C's definition would be incredibly unpleasant to use with no implicit conversions from void * . If you had to write things like int * x = (int *)NULL I'm pretty sure everyone would just use 0.
Re:Issues with porting C++ code to a 64-bit platform by CamperBob2 on Monday April 01, 2013 @04:04PM (#5475530)
I'm not convinced that the explosion of specialized types benefits anyone but language lawyers. Some of us actually have work to do, and NULL should mean NULL.

Using '0' for null pointers as mpyne suggests is less desirable because it robs the reader of context ("Is this a pointer being compared to 0, or a numeric type?") That could be worked around with Hungarian notation, but some would argue that the cure is worse than the disease.

Re:Issues with porting C++ code to a 64-bit platform by to3m on Monday April 01, 2013 @04:13PM (#5475590)
But before nullptr, NULL didn't portably mean "a null pointer". It meant "0", just as if you'd typed in "0" rather than "NULL". The language doesn't really give the library writer much choice in the matter! (Defining NULL as "((void *)0)" would at least give it an inherently pointer type, but it would be terribly inconvenient to use.)

This is the whole reason for nullptr in the first place.

Re:Issues with porting C++ code to a 64-bit platform by CamperBob2 on Monday April 01, 2013 @04:52PM (#5475855)
Right, I'm saying that as long as they were going to redefine NULL as part of the C++ standard anyway, they should have redefined it as ((void *) 0). That would be portable, at the expense of breaking code that used NULL in integer comparisons.
Re:Issues with porting C++ code to a 64-bit platform by vinkelhake on Monday April 01, 2013 @05:17PM (#5475982)
C++ doesn't have an automatic conversion from void* to T* so having NULL as ((void*)0) wouldn't have worked.
Re:Issues with porting C++ code to a 64-bit platform by CamperBob2 on Monday April 01, 2013 @05:41PM (#5476123)
True, but again, the whole problem is all of these weird, random changes and exceptions that only a small subset of programmers can keep in their heads. If you're going to go down that path, you should pick safe weird, random changes and exceptions.

In this case, they should have turned NULL into a first-class keyword that would parse as an invalid pointer of any given type, and called it "done." In the original C spec NULL wasn't supposed to be a pointer at all -- it was just a more polite way to write "zero." As endless Internet debates have shown, it turns out that it would have been better to reserve it for use in pointer expressions.

Re:Issues with porting C++ code to a 64-bit platform by mpyne on Monday April 01, 2013 @05:57PM (#5476217)
Turning a macro defined in probably hundreds of header files into a keyword would be a very bad idea for backward compatibility. It would have been nice if C had reserved it for use as a pointer and C++ had been coded from the beginning to allow NULL to be defined to any pointer type, but that wasn't how it ended up.

But what library vendors can do now is to simply

    #ifdef __cplusplus
    #if /* version check for C++11 */
    #undef NULL
    #define NULL nullptr
    #endif
    #endif
And that way there's less stuff to memorize, but still requires that the code itself has to opt-in, instead of the language having to figure out what insanity NULL may have been defined to.
Re:Issues with porting C++ code to a 64-bit platform by jws on Monday April 01, 2013 @02:49PM (#5475069)
Heh. Sometime around '94 I helped Southwestern Bell Telephone with that during the 16 bit to 32 bit transitions of a Mac client.

This means that sometime around the mid 2030's some poor programmers will have to take a break from worrying about Y0x80000000 to get bit by the x86-128 varargs bugs.

Re:Issues with porting C++ code to a 64-bit platform by DerekL on Monday April 01, 2013 @03:14PM (#5475234)
I'm not sure what you mean by the 16-to-32 transition on the Mac. Are you referring to the 68k-to-PowerPC transition?
Re:Issues with porting C++ code to a 64-bit platform by derleth on Monday April 01, 2013 @03:48PM (#5475420)
Initially, I thought it was the transition to 32-bit-clean code in the 68k line, but that was more of a 24-to-32 transition, and it only affected pointers:

http://lowendmac.com/trouble/32bit.shtml

Re:Issues with porting C++ code to a 64-bit platform by bitwize on Monday April 01, 2013 @03:39PM (#5475364)
And this is why you use nullptr, not NULL.
Re:Issues with porting C++ code to a 64-bit platform by mpyne on Monday April 01, 2013 @03:55PM (#5475468)
Yes, this is a great reason to have nullptr, but when this occurred there wasn't even such a thing as -std=c++0x. ;)