Thread: Optional Function Parameters in Prototypes

  1. #16
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    thats not easier then including <iostream>.. im asking why is 'cstddef' 'better'

  2. #17
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Using <cstddef> is better because that is where the standard says that NULL is defined. If you use iostream, it may work for you, but it might not work when you switch compilers, it might not work if you give your code to somebody else, and it might not work if you post code here for somebody to help you with and they try to compile it on their compiler.

    Of course, it really doesn't matter because unless you are writing code that needs to work in C also, you should just use 0.

  3. #18
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Probably because it is not standard for iostream to include the file that has the definition of
    NULL. I don't feel like digging in the standard to make sure, but this is most likely the case. Which means this may or may not work on all compilers.

    EDIT: Pwned by Daved.

  4. #19
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    ive never had to use cstddef so since i usually have iostream already included i dont have to do anything else.. if i ever need NULL and dont have/need iostream then as daved said earlier 0 is the easiest and most portable way.

  5. #20
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Including cstddef and using NULL is more readable in some cases, and same portability. Using 0 and NULL is the same, but using NULL just by including <iostream> is non standard. Stick to the standard when you can, makes life easier for everyone, besides the extra effort of typing out #include <cstddef> is nothing compared to debug time in a programs lifetime.

  6. #21
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    rather than having to include a whole header you dont need, would it also be ok to just #define it yourself?

  7. #22
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    No, because you could easily get multiple definition warnings that are extremely annoying.

  8. #23
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    but if i dont have a header file that has defined it previously, ie if i dont #include<cstddef> then its not defined, so if i use #define for NULL then itd be the first time the compiler would 'see' it... no?

  9. #24
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You'd what to put include gaurds in. Header files are ment to work with more than one sourse file, so just because one sourse file doesn't define null, dosen't mean another will.

    You'd have to do this:
    Code:
    #ifndef NULL
    #define NULL ((void *) 0)
    #endif
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  10. #25
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    This is C++ NULL should be defined as 0... see my above post (near the start of this thread).

  11. #26
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i was looking in the stddef.h file (where this is actually defined) and it looks like they have both defined, so he just missed the #define for the value of 0.

    Code:
    /* Define NULL pointer value */
    
    #ifndef NULL
    #ifdef __cplusplus
    #define NULL    0
    #else  /* __cplusplus */
    #define NULL    ((void *)0)
    #endif  /* __cplusplus */
    #endif  /* NULL */
    edit: oh i see what your saying, for C++ the standard is 0 but for C its ((void*)0)

  12. #27
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Right, I was just making sure that he used the right one, the one he posted is the C style.

  13. #28
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Of course, why would you go through any of that trouble when you should just use 0 anyway.

    Luckily, in a few years everybody will start using nullptr instead so at hopefully at some point it will be consistent.

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. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  4. Replies: 5
    Last Post: 02-08-2003, 07:42 PM