'NULL' undeclared (first use this function)

This is a discussion on 'NULL' undeclared (first use this function) within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by MarkZWEERS But Laserlight, Stroustrup seems to say that he prefers using '0' instead of 'NULL' for it ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,711
    Quote Originally Posted by MarkZWEERS
    But Laserlight, Stroustrup seems to say that he prefers using '0' instead of 'NULL' for it is a macro, while you are using this argument to use NULL instead of 0?
    Whoops, you are right: I made a typo error. (Two, actually. For some reason, I first wrote "NULL for NULL specific", then replaced the second "NULL" with "C++" but forgot to replace the first with "0". This NULL business is very strange.)

    Quote Originally Posted by Yarin
    I use null to make the code more readable. When I see 0, I think "an int", when I see NULL, I think "a pointer".
    The problem is that the type says otherwise (NULL is an integer), so we have to wait for nullptr to come into play to avoid getting misled while getting extra readability.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Could someone refresh my memory... Why didn't C++ define NULL as (void*)0 ?

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    Could someone refresh my memory... Why didn't C++ define NULL as (void*)0 ?
    Because you can't SET a pointer of arbitrary type of the value of a void pointer. In C it's valid to copy void pointers in both directions, but C++ only allows "any" to void, but not void to any.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #19
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by matsp View Post
    Because you can't SET a pointer of arbitrary type of the value of a void pointer. In C it's valid to copy void pointers in both directions, but C++ only allows "any" to void, but not void to any.

    --
    Mats
    But it does allow int to pointer... that doesn't seem very consistent with that logic. At least void* is actually a pointer.
    How is that nullptr going to work then? Is it going to be something like this (if this code was actually legal):
    Code:
    template <typename T> T* nullptr = 0;

  5. #20
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    Seeing as it's supposed to be a language element (built-in type), I don't think it isn't going to be defined anywhere. This is from what I understand anyway.
    And regardless of whether NULL is an integer or not, I always stick it to pointers and thus define it as a pointer that points to nothing. We don't need to see the type "behind" it. We only need to see the word as a meaning.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by cpjust View Post
    But it does allow int to pointer... that doesn't seem very consistent with that logic. At least void* is actually a pointer.
    How is that nullptr going to work then? Is it going to be something like this (if this code was actually legal):
    Code:
    template <typename T> T* nullptr = 0;
    But it only allows ONE integer value: zero! Try setting a pointer to (for example) 1, 7, 42 or -1 without a reinterpret_cast<T *>(n), and it will give you a compiler error.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #22
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    nullptr will be of the special type nullptr_t and have very special rules regarding conversions.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #23
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,246
    We only need to see the word as a meaning.
    The compiler doesn't see it as having meaning; we need a 'nullptr' that really isn't an integer type.

    Code:
    template <typename T> T* nullptr = 0;
    ;_;

    This pretty much works until compilers provide:

    Code:
    struct nullptr_t {template<typename T> operator T * (){return(0);}} nullptr;
    Soma

  9. #24
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    What about this? . . . .
    Code:
    void *p = &nullptr;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #25
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,246
    What about this?
    I did say that it "pretty much works". It is easy to disable the address operator or induce a compiler error with any given message you might like. The problem of implementing 'nullptr_t' as proposed by the standard is a problem exclusively do to the unusual value semantics imposed by the throw/catch portion of the proposal. If you are going to offer a complaint, complain about that; it can't be match by any library tricker.

    All of the "compiler diagnostic" stuff is a kind of "red herring". Compilers need better error messages for templates anyway; giving them an excuse not to improve in the area isn't a good thing.

    Soma

  11. #26
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    Quote Originally Posted by phantomotap View Post
    The compiler doesn't see it as having meaning; we need a 'nullptr' that really isn't an integer type.
    The only thing I see bad about NULL is that it can't be reliably overloaded.
    You'd be overloading the whole int-range and that means any integer would be accepted.
    Although the compiler might be able to do an implicit conversion to a T* type, but I'm not sure how well it works with overloaded operators...

    I don't see NULL as wrong at all, but I would love to see it as a non-integer type, so that it can only be applied to pointers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #27
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    0 is c++:NULL is c.
    It's as simple as that.

  13. #28
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by lruc View Post
    0 is c++:NULL is c.
    It's as simple as that.
    So if I see the following line in a piece of code:
    Code:
    if (ptr == 0)
    I can assume the code is not written in C?

  14. #29
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    In windows
    Code:
    #include <windows.h>
    In everything else
    Code:
    #define NULL 0
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  15. #30
    Registered User
    Join Date
    Jun 2008
    Posts
    266
    I see your point swoopy but you know what I meant. Sorry if I didn't elaborate enough.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21