Is assigning null to non pointers considered wrong or improper in standard c++ code?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • challa
    New Member
    • Jul 2020
    • 2

    Is assigning null to non pointers considered wrong or improper in standard c++ code?

    Hello everyone,,

    `int x = NULL`

    Is this considered wrong or bad? and if so why?

    I used to write 'using namespace std;' in my code because that's what most textbooks and classes did, but I found a nice post on a forum explaining why its bad and the reason for so. So it made me wonder if making a none pointer NULL was bad.

    What if the int could be 0, or any other number? How could we check if a int was empty then?


    `Object x = NULL;`

    Is it okay to set objects equal to NULL, or should objects be initialized setting their data members to 0 or empty string, depending on their data types?
  • dev7060
    Recognized Expert Contributor
    • Mar 2017
    • 656

    #2
    `int x = NULL`

    Is this considered wrong or bad? and if so why?

    I used to write 'using namespace std;' in my code because that's what most textbooks and classes did, but I found a nice post on a forum explaining why its bad and the reason for so. So it made me wonder if making a none pointer NULL was bad.
    Depends. NULL is defined as (void *)0 in C. In C++ it's a macro. Write some code and see the self-explanatory warnings yourself.
    `Object x = NULL;`

    Is it okay to set objects equal to NULL, or should objects be initialized setting their data members to 0 or empty string, depending on their data types?
    What's the relationship between setting an object to NULL and initializing individual members? Two completely different things.
    What if the int could be 0, or any other number? How could we check if a int was empty then?
    A variable is the name of a memory cell. Do you ever say if a cell is empty or not? Because it never is.

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      You should be using nullptr not NULL in modern C++. It has better type safety.

      It is also good understand what is going on, in C and C++ when the value 0 is used in a pointer context when the compiler is required to transform that value to the null pointer representation of the underlying platform. Note that for a large number of platforms the null pointer representation is the value 0 but it does not have to be. The macro NULL was introduced into C (and carried over into C++) as a shortcut so that programmers could make it obvious in the code when they were using the null pointer rather than the value 0.

      In C NULL was defined as
      Code:
      #define NULL (void *)0
      This works because in C you can implicitly convert between most integer types and pointers at their base level (certainly in C) are just a integer type with some additional semantics about how you interpret them.

      The along came C++ with its improved type safety, pointers stopped being related to integers and also stopped being related to each other if they pointed to different things so this code
      Code:
          void *x = 0;
          int * y = x;
      compiles fine in C (to this day) but causes an error in C++ at the second line because you can't assign void * to int *.

      So they changed the definition of NULL to
      Code:
      #define NULL 0
      and because the compiler is require to convert the value 0 used in a pointer context to the null pointer representation for the platform that worked.

      But now NULL is just the value 0 which is valid in many contexts so
      Code:
          void *x = NULL;
          int y = NULL;
          double z = NULL;
      All compile (although modern compilers tend to look for NULL and issue warnings) because
      Code:
          void *x = 0;
          int y = 0;
          double z = 0;
      Are all valid statements.

      The C++ community felt it could do better and came up with the keyword/type nullptr. This indicates to the compiler that you are intending to use a nullptr, the standard requires the compiler to implicitly convert nullptr to the pointer type required for the current context so
      Code:
          void *x = nullptr;
          int *y = nullptr;
          double *z = nullptr;
      All compile but
      Code:
          int *y = nullptr;
          double *z = nullptr;
      
          int v = nullptr;
          z = y;
      issues errors on lines 4 and 5.

      So were does all this leave us?
      Well if you want to set an integer to 0 then use 0, not NULL NULL is for pointers and an integer is never NULL semantically as the context of NULL means has no valid value and 0 is a valid value for an integer; similarly for floating point types use 0.0.

      If you want to set a pointer to the null pointer then use nullptr, it has better type safety and is in all ways better than NULL.

      But also do not consider the value of the null pointer, do not think I am setting the pointer to 0 because depending on the platform you may not be doing that always think I am setting the pointer to the null pointer representation (or just null pointer) without worrying about what that value actually is.

      Comment

      Working...