Schrodinger's NULL?



  • After lurking on tdwtf for quite a while, i've finally had the pleasure or misfortune to join a project that will give me reasons to post on this forum for the following few months.

    Anonymized code snippet:

    if (count >= (*some_ptr)[some_index]) {                                                                                             
       if (some_ptr == NULL)                                                                                                       
          return (true);
      ... rest of function ...
    }
    

    For the curious, some_ptr is a global pointer to a STL map object.



  • I'm assuming that they haven't redefined NULL as something else.

     Maybe it's a multithreading issue.



  • TRWTF is that you're accessing the STL map object via a global pointer.  You should really use a helper function to return a reference to the hopefully otherwise inaccessible map, that way it can never be null.

    And, DescentJS, if it is because of a multithreading issue, that's definitely the wrong way to solve it.  (The pointer could just as easily change just after the if statement.)



  • @Heron said:

    And, DescentJS, if it is because of a multithreading issue, that's definitely the wrong way to solve it. 

    I don't think this is the right way to solve any problem. 

    @Heron said:

    (The pointer could just as easily change just after the if statement.)

    Of course, that's why it's on here.



  • @torp said:

    Schrodinger's NULL?
    It's Schrödinger. Other than that, nice code snippet.



  • @derula said:

    @torp said:
    Schrodinger's NULL?
    It's Schrödinger. Other than that, nice code snippet.

    Maybe it was Schrödinger's umlaut?



  • @torp said:

    (count >= (*some_ptr)[some_index])
     

    The behaviour of this part is rather unpredictable for some_ptr==NULL. It could crash the program,  it could evaluate to true, or it could evaluate to false, so the second line might actually make some sense.

    Of course, sane people would put the NULL check first, for the same reason.



  • @bannedfromcoding said:

    @derula said:
    @torp said:
    Schrodinger's NULL?
    It's Schrödinger. Other than that, nice code snippet.
    Maybe it was Schrödinger's umlaut?

     

    You've coined a potentially useful term here.  I'm going to use "Schrödinger's umlaut" to describe words that may or may not have diacriticals, depending on laziness or keyboard layout.



  • @Minos said:

    I'm going to use "Schrödinger's umlaut" to describe words that may or may not have diacriticals, depending on laziness or keyboard layout.

    I'm pretty sure that first person which will combine the term with a snippet of <favourite programming language> that displays "ö" or "o" randomly will win a bucketful of internets... or at least become famous.



  • @Minos said:

    @bannedfromcoding said:

    @derula said:
    @torp said:
    Schrodinger's NULL?
    It's Schrödinger. Other than that, nice code snippet.
    Maybe it was Schrödinger's umlaut?

    You've coined a potentially useful term here.  I'm going to use "Schrödinger's umlaut" to describe words that may or may not have diacriticals, depending on laziness or keyboard layout.

    I am working on a major expose.  I have discovered a cafe in Curacao that is a veritiable smorgasbord of secrets.  The pho contains moldy jalapenos, the flambe uses lighter fluid,  and the consumme is nothing but water.  Imagine abusing the naivite of your clientele so boldly!  But the coup de grace is this: I caught the maitre d' having a menage a trois with the chef and the owner's fiancee (who was wearing a rather revealing negligee), right in the kitchen!  I was just about to snap a few photos when the garcon -- a stereotypical Quebecois if I've ever seen one -- caught me.  Needless to say there was a bit of a melee, but I beat him within an inch -- nay, an angstrom! -- of his life.  Cliched as it is, I will fashion a disguise using papier-mache and return tomorrow as the doppelganger of one of the busboys.  I am going to break this story wide open; I'll tear through their facade like crepe paper!  I'm telling you, this will be a cause celebre; the piece de resistance for my resume that gets me hailed as an Ubermensch.


  • @Minos said:

    @bannedfromcoding said:

    @derula said:
    @torp said:
    Schrodinger's NULL?
    It's Schrödinger. Other than that, nice code snippet.

    Maybe it was Schrödinger's umlaut?

     

    You've coined a potentially useful term here.  I'm going to use "Schrödinger's umlaut" to describe words that may or may not have diacriticals, depending on laziness or keyboard layout.

    In my defense, i'm neither English nor German and i don't have the keyboard set up even for my own language's diacriticals ;)



  • @ammoQ said:

    @torp said:

    (count >= (*some_ptr)[some_index])
     

    The behaviour of this part is rather unpredictable for some_ptr==NULL. It could crash the program,  it could evaluate to true, or it could evaluate to false, so the second line might actually make some sense.

    Of course, sane people would put the NULL check first, for the same reason.

    And sane compilers, when optimising, will say to themselves"WTF?  If he's already dereferenced it, it can't be NULL (unless it's volatile) or we would have crashed before now", and optimise away the NULL test.

    Of course, on a small embedded device, with or without a filing system, there's always -fno-delete-null-pointer-checks.




  • @torp said:

    After lurking on tdwtf for quite a while, i've finally had the pleasure or misfortune to join a project that will give me reasons to post on this forum for the following few months.

    Anonymized code snippet:

    if (count >= (*some_ptr)[some_index]) {                                                                                             
    if (some_ptr == NULL)
    return (true);
    ... rest of function ...
    }

    For the curious, some_ptr is a global pointer to a STL map object.

    It was necessary to specify that.  There's no reason in principle that you couldn't create a self-NULLing "may be dereferenced once only" 'smart' pointer class in C++, although god knows what anyone would actually want to do that for.




  • @DaveK said:

    @torp said:

    After lurking on tdwtf for quite a while, i've finally had the pleasure or misfortune to join a project that will give me reasons to post on this forum for the following few months.

    Anonymized code snippet:

    if (count >= (*some_ptr)[some_index]) {                                                                                             
    if (some_ptr == NULL)
    return (true);
    ... rest of function ...
    }

    For the curious, some_ptr is a global pointer to a STL map object.

    It was necessary to specify that.  There's no reason in principle that you couldn't create a self-NULLing "may be dereferenced once only" 'smart' pointer class in C++, although god knows what anyone would actually want to do that for.


     To protect against data piracy of course.



  • Nice comments, but never attribute to malice that which can be adequately explained by incompetence :)



  • @bannedfromcoding said:

    @derula said:
    @torp said:
    Schrodinger's NULL?
    It's Schrödinger. Other than that, nice code snippet.
    Maybe it was Schrödinger's umlaut?
    You are an evil man, and for that, you get a gold star.



  • @bstorer said:

    I am working on a major expose.  I have discovered a cafe in Curacao that is a veritiable smorgasbord of secrets.  The pho contains moldy jalapenos, the flambe uses lighter fluid,  and the consumme is nothing but water.  Imagine abusing the naivite of your clientele so boldly!  But the coup de grace is this: I caught the maitre d' having a menage a trois with the chef and the owner's fiancee (who was wearing a rather revealing negligee), right in the kitchen!  I was just about to snap a few photos when the garcon -- a stereotypical Quebecois if I've ever seen one -- caught me.  Needless to say there was a bit of a melee, but I beat him within an inch -- nay, an angstrom! -- of his life.  Cliched as it is, I will fashion a disguise using papier-mache and return tomorrow as the doppelganger of one of the busboys.  I am going to break this story wide open; I'll tear through their facade like crepe paper!  I'm telling you, this will be a cause celebre; the piece de resistance for my resume that gets me hailed as an Ubermensch.
     

    Must have been the café near Asiento, sounds like I remember it. ;-)


Log in to reply