Evaluating a NULL pointer

This is a discussion on Evaluating a NULL pointer within the C Programming forums, part of the General Programming Boards category; Hello, Why is it better to do the following if (NULL != my_pointer) then if (my_pointer != NULL)? My boss ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    20

    Evaluating a NULL pointer

    Hello,

    Why is it better to do the following

    if (NULL != my_pointer)

    then

    if (my_pointer != NULL)?

    My boss says that if my_pointer is NULL and C evaluates a null
    pointer first it will crash, but isn't it true that either way a null
    pointer will be evaluated?

    Thanks a lot.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    neither of the examples you posted would create a problem. a pointer is just a variable that holds an address. the problem arises when you try to access data at an invalid address (by dereferencing the pointer).
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    498
    Hmm... never heard that one before. I tried it, and the program didn't crash. Just do what the boss says. Keep the person who gives you your paycheck happy. ;-)

  4. #4
    Mad OnionKnight's Avatar
    Join Date
    Jan 2005
    Location
    Umeň, Sweden
    Posts
    555
    You sure your boss wasn't thinking of the difference between NULL = my_pointer and my_pointer = NULL?

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by alois_rone
    Why is it better to do the following
    In the stone age, compilers didn't let you know that you might be screwing up if you accidentally wrote this
    Code:
    if(my_pointer = NULL)
    instead of this
    Code:
    if(my_pointer == NULL)
    So writing it the other way around was a way to make up for using poor tools.
    Last edited by Dave_Sinkula; 02-18-2006 at 08:44 PM. Reason: Which is just another way of stating what OnionKnight mentioned.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,671
    Quote Originally Posted by Dave_Sinkula
    In the stone age, compilers didn't let you know that you might be screwing up if you accidentally wrote this
    Code:
    if(my_pointer = NULL)
    instead of this
    Code:
    if(my_pointer == NULL)
    So writing it the other way around was a way to make up for using poor tools.
    ..... even now, "if (my_pointer = NULL)" can in some (admittedly rare) cases be what the programmer intends. So, few compilers will flag this construct as an error by default, but they may give a warning. A few too many programmers ignore warnings or tweak compiler settings to turn warnings off. Those same people then complain about the language or about the tools when they get in trouble .......

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    Quote Originally Posted by Dave_Sinkula
    In the stone age, compilers didn't let you know that you might be screwing up if you accidentally wrote this
    Code:
    if(my_pointer = NULL)
    MSVC wont give you an error unless you increase the warning level to max. And honestly, why would I want the compiler to give me a warning every time I did something like:

    Code:
    if(i = foo())
    {
       ...
    }

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by bithub
    And honestly, why would I want the compiler to give me a warning every time I did something like:

    Code:
    if(i = foo())
    {
       ...
    }
    Because it could easily and perhaps more clearly be written like this?
    Code:
    error = foo(); /* do something */
    if ( error ) /* test result of doing something and act appropriately */
    {
       ...
    }
    To each his own. Tailor your tools to your style and whatever best allows you to write clear, correct, maintainable code. I have my preferences; so does everybody else. I want lint's "Boolean test of assignment" diagnostic to appear because it does catch bugs, and I generally write code to avoid it.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    20

    Evaluating a NULL pointer

    Thanks so much for the responses. My boss is a fresh grad... and I am a senior engineer , but new to C, so maybe I shouldn't take ALL his comments too seriously.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's good to check things for yourself, get as much information on things as you can. Being informed is always a good thing.
    Quote Originally Posted by alois_rone
    so maybe I shouldn't take ALL his comments too seriously.
    I agree. To illustrate the point, we have a "author / teacher" here who most of us agree is full of ........ most of the time.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by alois_rone
    Hello,

    Why is it better to do the following

    if (NULL != my_pointer)

    then

    if (my_pointer != NULL)?

    My boss says that if my_pointer is NULL and C evaluates a null
    pointer first it will crash, but isn't it true that either way a null
    pointer will be evaluated?

    Thanks a lot.

    I've seen that construct CONSTANT != variable or CONSTANT == variable before and it is usually used as a way of having your compiler catch something like this:

    Code:
        /* Mistake 1 -- compiler won't catch it */
    
        if (myvar = CONSTANT) {
            /* do something here */
        }
    If you look closely, you will see that becuase of a typo, the if statement is really setting the value of myvar to CONSTANT, instead of checking for equality (==).

    If you flip the expression around
    Code:
        /* Mistake 2 -- compiler will */
        if (CONSTANT = myvar) {
    the compiler will gak on it because you cannot change the value of a constant.

    Because the first mistake up there is perfectly legal C, it'll cause funky runtime errors that might be hard to find.


    Personally, I never use it -- causes too much confusion with new programmers <grin>

  12. #12
    Registered User
    Join Date
    Feb 2006
    Posts
    20
    Thanks again. Yeah I think that's the real reason behind ('=' instead
    of '=='), although my boss got the wrong explanation You guys are very helpful!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Virtual Printer" or "Moving Printjobs"
    By extasic in forum Windows Programming
    Replies: 12
    Last Post: 06-30-2011, 09:33 AM
  2. addrinfo Structure Not Working
    By pobri19 in forum Networking/Device Communication
    Replies: 9
    Last Post: 10-22-2008, 11:07 AM
  3. Replies: 7
    Last Post: 06-16-2006, 10:23 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM

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