Wish I'd caught this when I ported the library.



  • A few weeks ago I ported a C++ library from one sub-product to the mainline. Basically, it's a static library used to simulate the functionality of an AN/PAS-13(C) weapon sight. Nothing particularly fancy, you tell it what button was pressed, it triggers events.


    Today I was working on a new library in the mainline and wanted to check something in the other library to make sure I was keeping the interface remotely similar. I opened up the header file and this jumped out at me:


    [code]
    #pragma once


    #include <string>


    #ifndef NULL

    #define NULL 0

    #endif

    [/code]

    LOL WUT?


    Wish I'd spotted that when I did the port, although to be honest all I did was copy the project over and add it to the solution. I'm actually somewhat worried about what other gems are hiding in here...


    Yeah, I know, not really a WTF in the regard of it breaking things, but I think if we ever run into an instance that NULL isn't NULL, we've got other problems.



  • NULL defined as 0?

    AndyCanfield been at your code?



  • I don't get it.
    <font face="Lucida Console" size="2">#define NULL 0</font>
    is pretty standard in C++ (in plain C it is typically (void*)0).



  • Um, what's the problem? Is there any case where NULL is NOT 0?



    My only nitpick is that it should probably be #define NULL ((void*)0)


  • Discourse touched me in a no-no place

    @pkmnfrk said:

    Um, what's the problem? Is there any case where NULL is NOT 0?
    Typically in C where it can be ((void*)0)
    @pkmnfrk said:
    My only nitpick is that it should probably be #define NULL ((void*)0)
    Not in C++ it shouldn't.



    http://stackoverflow.com/a/7016898



  • @pkmnfrk said:

    Um, what's the problem? Is there any case where NULL is NOT 0?
    How about when it hasn't been defined yet?

    NB: at least on my system, <string>  includes <stddef>, which #defines NULL.



  • @pkmnfrk said:

    Is there any case where NULL is NOT 0?

    Yes.

    If someone comments out that #define in stddef.h.

    Yes.

    It actually happened.

    Sigh.



  • @CodeNinja said:

    Yeah, I know, not really a WTF in the regard of it breaking things, but I think if we ever run into an instance that NULL isn't NULL, we've got other problems.

    But the code is there for the case when NULL is not defined.


    And since C++ doesn't specify a single well-defined place where NULL is #defined, this is one quite reasonable compiler-independent way to make sure it gets defined.



  • @Hatshepsut said:

    @CodeNinja said:
    Yeah, I know, not really a WTF in the regard of it breaking things, but I think if we ever run into an instance that NULL isn't NULL, we've got other problems.

    But the code is there for the case when NULL is not defined.


    And since C++ doesn't specify a single well-defined place where NULL is #defined, this is one quite reasonable compiler-independent way to make sure it gets defined.




    While true, like was said earlier, it includes string right above the #ifndef, which includes files that eventually includes stdio.h, where NULL is defined. So, really, it's impossible for that #ifndef to ever hit, considering we're running only on well-known Windows boxes which we provide to the user and have complete control over what service packs and APIs are installed, and it's always built in Visual Studio.


    Funniest part is, when I asked the original author about it, he went off on a rant about another engineer who left before he did who he claimed added it, until I pointed out that it was in version 1 of the file on Surround and he checked that in. At which point he shifted gears to defending it.


    Needless to say I don't really miss working with him, although I do wish him all the luck in his new venture.



  • @CodeNinja said:

    @Hatshepsut said:
    @CodeNinja said:
    Yeah, I know, not really a WTF in the regard of it breaking things, but I think if we ever run into an instance that NULL isn't NULL, we've got other problems.

    But the code is there for the case when NULL is not defined.


    And since C++ doesn't specify a single well-defined place where NULL is #defined, this is one quite reasonable compiler-independent way to make sure it gets defined.




    While true, like was said earlier, it includes string right above the #ifndef, which includes files that eventually includes stdio.h, where NULL is defined. So, really, it's impossible for that #ifndef to ever hit, considering we're running only on well-known Windows boxes which we provide to the user and have complete control over what service packs and APIs are installed, and it's always built in Visual Studio.

    Ah - in such a deterministic context, I agree you're right.


    If the code was trying to be portable it would be a different story. (And perhaps the whole thing is cargo cult legacy originating in just such a situation.)


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.