Thread: Null pointer naming

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    2

    Null pointer naming

    Hi

    This post is regarding the proposed name for the null pointer - nullptr (see http://www.open-std.org/jtc1/sc22/wg...004/n1601.pdf).
    I am wondering why it has not been considered using the already existing keyword "void" as the name for the null pointer.

    In my head the word void perfectly describes what a null pointer is pointer at - namely absolutely nothing. And since it already is a keyword it has the benefit of not introducing problems with existing code. On the downside it does make the "void" keyword more ambiguous.

    So my question to the board is: Is there any technical (compiler) reason for not using this keyword to assign a null value to a pointer?

    regards
    als

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >So my question to the board is: Is there any technical (compiler)
    >reason for not using this keyword to assign a null value to a pointer?
    Code:
    int main()
    {
      foo ( void );
    }
    What is foo?
    My best code is written with the delete key.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    *head asplodes after trying to answer Prelude's question*
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User
    Join Date
    Jun 2006
    Posts
    1
    i hate foo

  5. #5
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    it's just a junk name

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    well, foo is a compile-time error
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Anyways, the reason you can't is because void is a reserved word. You cannot use reserved words to name your objects.

    You can name your pointer Void though... but if you do I hope you burn in hell
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Anyways, the reason you can't is because void is a reserved word.
    I think the question was regarding a proposed addendum to the standard. Rather than defining a new reserved word, the OP's suggestion is to simply reuse void. It's not a bad question, but aside from being terribly ambiguous in certain situations, it would break a *lot* of existing code. It also introduces countless special cases for an implementation to differentiate between the void type and the void object; which a new reserved word could easily avoid.
    My best code is written with the delete key.

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Oh, I see your point. Just read the document.

    Well... let me see if I understand the meaning of your initial question then... What is foo?

    Answer: Foo is ambiguous. It can be a function declaration or a function call.

    Is that it?
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  10. #10
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    How about a new keyword "Is" and a new keyword "Nothing". That way, you could code something like this:

    Code:
    int main() {
        void* vp = Nothing;
    
        if (vp Is Nothing) {
            cout << "It's nothing, really." << endl;
        }
        return 0;
    }
    Also, consider what would happen if we added the keywords "Then" and "End If" to be used instead of the opening and closing curly braces. Also, the parens around the if condition are really unneeded, so lets say they are optional. Now we have:

    Code:
    int main() {
        void* vp = Nothing;
    
        if vp Is Nothing Then
            cout << "It's nothing, really." << endl;
        End If
        return 0;
    }
    Next we can get rid of semi-colons, as long as we support line-continuation characters. After all, usually you fit an entire statement on a single line anyway, so now this gets rid of a bunch of ugly and unnecessary characters in the code:

    Code:
    int main() {
        void* vp = Nothing
    
        if vp Is Nothing Then
            cout << "It's nothing, really." << endl
        End If
        return 0
    }
    Next lets say that we want to get rid of curly braces in general, in favor of something a little more descriptive. I propose something like this:

    Code:
    Function main() As integer {
        void* vp = Nothing
    
        if vp Is Nothing Then
            cout << "It's nothing, really." << endl
        End If
        return 0
    End Function
    Also, if we can get rid of the void*, that would be nice also. Maybe we can come up with some kind of keyword that just means we're declaring a variable, without actually specifying the type (that's basically what void* is anyway). And then declaring the type would be optional. Ok so our new keyword will be...oh I don't know how about "Dim". Could be anything really, but just for illustrative purposes...so lets see what we have now:


    Code:
    Function main() As integer {
        Dim vp = Nothing
    
        if vp Is Nothing Then
            cout << "It's nothing, really." << endl
        End If
        return 0
    End Function
    Ok, we're almost done. But what should we do with cout? I really don't like those << and >>. I mean, is that some kind of bit-shift operation or what's going on? I think we would be better off if there was some kind of object that had a WriteLine function, just so we were clear on exactly what's happening. Maybe make a Console object...and put that in a System namespace. Ok, so here it is:

    Code:
    Function main() As integer {
        Dim vp = Nothing
    
        if vp Is Nothing Then
            System.Console.WriteLine("It's nothing, really.")
        End If
        return 0
    End Function
    There, that looks good. Maybe we should ship this off to a c++ comittee to see if they can't do something with it.
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  11. #11
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Foo is a macro that resolves to a prototype for a function bar:

    Code:
    # define foo(X) X bar(X)
    Actually I thought that foo could be a function declaration (or maybe even a function call to a function that takes no parameters), but gcc complains:

    out.c:2: error: syntax error before "void"

    So I guess that it has to be a function call taking void, which in this case would be NULL.

    Just out of curiosity, what is the reasoning for why we need a keyword for NULL? Isn't NULL good enough?

    And by the way, wow, I just realized that C/C++ is a nutty language. Because actually what I realized is that foo(void); is really a prototype for this function: int bar(int x). Check this out:

    Code:
    # define foo(X) X bar(X);
    # define void int
    
    int main() {
        foo(void);
        return 0;
    }
    
    int bar(int x) {
        printf("Called foo\n");
        return 0;
    }

    It seems like redefining keywords should be disallowed...
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I believe Prelude asked that in the context of void being a null pointer.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #13
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    Yes, she did, but in order for foo(void) to be ambigous (when void is also NULL), it would have to have some legitimate meaning in the normal c/c++ sense. Wouldn't it?
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Isn't NULL good enough?
    Not in C++. NULL is a symbolic constant for the integer 0. Consider an overloaded function that can take either a pointer or an integer and you'll see one of the problems with NULL in C++. There's a cult out there that gets their kicks from thinking of ways to have a true null pointer object. I won't deny that it's an interesting, if silly, exercise, but it's been explored to death at this point.
    My best code is written with the delete key.

  15. #15
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    But if you realize it was in the context of void meaning a null pointer, why did you test this on your compiler? It is obvious your compiler cannot be used. As far as it is concerned, void is not a null pointer.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. addrinfo Structure Not Working
    By pobri19 in forum Networking/Device Communication
    Replies: 9
    Last Post: 10-22-2008, 10:07 AM
  2. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM
  3. Wierd Segmentation Faults on Global Variable
    By cbranje in forum C Programming
    Replies: 6
    Last Post: 02-19-2005, 12:25 PM
  4. button 'message'
    By psychopath in forum Windows Programming
    Replies: 12
    Last Post: 04-18-2004, 09:57 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM