Thread: Anyone else think it's kinda weird...

  1. #1
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352

    Anyone else think it's kinda weird...

    ...how when some functions return 0, it was a success, yet when others return NULL, it's a failure?
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    err, no?

    Depends on exactly the functions you are talking about here. But null and 0 are two very distinct things. And even when used in a return value to denote failure, they still have a distinct semantics that is quite intentional.

    Care to give an example of what functions you are talking about. Are we discussing C here, per chance?
    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.

  3. #3
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by Mario F. View Post
    err, no?

    Depends on exactly the functions you are talking about here. But null and 0 are two very distinct things. And even when used in a return value to denote failure, they still have a distinct semantics that is quite intentional.

    Care to give an example of what functions you are talking about. Are we discussing C here, per chance?
    Yeah, C. strcmp() and strncmp() return 0 if two strings are equal (success). And functions like malloc() and fopen() return NULL if there's not enough memory, or if the file couldn't be opened (failure). Last time I checked, NULL == 0. I thought that was pretty weird.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I'll give you a few minutes to think why's that...

    Think of what the function is returning exactly and what is the use you make of what is returned.
    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.

  5. #5
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Never mind.

    :P
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  6. #6
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by Babkockdood View Post
    Yeah, C. strcmp() and strncmp() return 0 if two strings are equal (success). And functions like malloc() and fopen() return NULL if there's not enough memory, or if the file couldn't be opened (failure). Last time I checked, NULL == 0. I thought that was pretty weird.
    That's because strcmp() doesn't check to see whether the two strings match, it compares their lexical value. Considering you probably don't know what that means, you really should read the manual on strcmp().

    As far as malloc() goes... what else could it possibly return on failure?

    Also, I would disagree about NULL != 0. In C(++) they are quite the same thing, often times even semantically. Calling zero, null, only implies that the variable holding the value in question, is probably a pointer.

    EDIT: Never mind..

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Babkockdood View Post
    Yeah, C. strcmp() and strncmp() return 0 if two strings are equal (success). And functions like malloc() and fopen() return NULL if there's not enough memory, or if the file couldn't be opened (failure). Last time I checked, NULL == 0. I thought that was pretty weird.
    NULL isn't 0 ... it's a pointer to 0... look it up!

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CommonTater
    NULL isn't 0 ... it's a pointer to 0... look it up!
    *facepalm*

    You should take your own advice...
    C: What is NULL and how is it defined?
    C++: Should I use NULL or 0?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    >> Also, I would disagree about NULL != 0. In C(++) they are quite the same thing, often times even semantically.[/quote]

    So, you are saying it's quite alright to write:

    if (myClass == 0) do_something();

    Maybe the word "semantics" has a different meaning to you and me. Coincidentally, this is an old, old, old, debate that I have no wish of starting with you. So if your answer to the above code is "that is perfectly alright", suit yourself.

    >> Calling zero, null, only implies that the variable holding the value in question, is probably a pointer.

    Ah!... I love the smell of uncertainty in the strongly-typed morning.
    Last edited by Mario F.; 07-25-2011 at 10:34 PM.
    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
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Mario F.
    So, you are saying it's quite alright to write:

    if (myClass == 0) do_something();
    Maybe, maybe not. It depends on what is myClass. If it is a class name, then that would result in a syntax error, so the answer to your question becomes blatantly obvious.

    Quote Originally Posted by Mario F.
    Ah!... I love the smell of Probably in the strongly-typed morning.
    nullptr FTW!
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by Babkockdood View Post
    ...how when some functions return 0, it was a success, yet when others return NULL, it's a failure?
    The same reason you can safely use a 0 for different things sometimes, but you can't safely dereference NULL ever. The rules for NULL aren't only a convention like returning 0 for success is.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by laserlight View Post
    *facepalm*

    You should take your own advice...
    C: What is NULL and how is it defined?
    C++: Should I use NULL or 0?
    Ahem... from stdio.h, Pelles C ver 6.50 rc4...
    Code:
    /* macros */
    #ifndef NULL
    #define NULL  ((void *)0)
    #endif
    Need it made any plainer?

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by CommonTater View Post
    Ahem... from stdio.h, Pelles C ver 6.50 rc4...
    Code:
    /* macros */
    #ifndef NULL
    #define NULL  ((void *)0)
    #endif
    Need it made any plainer?
    Only to ask why you think that's a pointer to zero, since it so obviously isn't.

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    Only to ask why you think that's a pointer to zero, since it so obviously isn't.
    You mean like "it's a macro that expands to a void pointer to 0"... like that?

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    It's not that either. It is the value 0, cast to a void*. It doesn't point to 0; it doesn't point anywhere (that is, after all, the point of NULL).

    (Edit to add: If you want to say that, in the case of a pointer, "having the value of" and "pointing to" are the same thing (i.e.
    Code:
    void *x = 0x100;
    where x has the value 0x100 and points to 0x100), that would be fine; however, that's not where we started this sidetrack....)
    Last edited by tabstop; 07-26-2011 at 06:53 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. kinda new need help
    By drkylec in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2011, 03:30 PM
  2. kinda aggrivating me
    By guyfromfl in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 03-31-2008, 10:54 AM
  3. I'm kinda new to C++
    By DeanDemon in forum C++ Programming
    Replies: 9
    Last Post: 12-01-2002, 12:52 AM