473,386 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

2
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?
Jul 23 '20 #1
2 1653
dev7060
636 Expert 512MB
`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.
Jul 23 '20 #2
Banfa
9,065 Expert Mod 8TB
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
Expand|Select|Wrap|Line Numbers
  1. #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
Expand|Select|Wrap|Line Numbers
  1.     void *x = 0;
  2.     int * y = x;
  3.  
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
Expand|Select|Wrap|Line Numbers
  1. #define NULL 0
  2.  
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
Expand|Select|Wrap|Line Numbers
  1.     void *x = NULL;
  2.     int y = NULL;
  3.     double z = NULL;
  4.  
All compile (although modern compilers tend to look for NULL and issue warnings) because
Expand|Select|Wrap|Line Numbers
  1.     void *x = 0;
  2.     int y = 0;
  3.     double z = 0;
  4.  
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
Expand|Select|Wrap|Line Numbers
  1.     void *x = nullptr;
  2.     int *y = nullptr;
  3.     double *z = nullptr;
  4.  
All compile but
Expand|Select|Wrap|Line Numbers
  1.     int *y = nullptr;
  2.     double *z = nullptr;
  3.  
  4.     int v = nullptr;
  5.     z = y;
  6.  
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.
Jul 23 '20 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: DevarajA | last post by:
I've read that 0 is the 'null pointer constant', and assigning it to a pointer makes it a 'null pointer'. Is that true? And is the opposite true? (assigning a null pointer to an int sets the int to...
2
by: Thomas G. Marshall | last post by:
Arthur J. O'Dwyer <ajo@nospam.andrew.cmu.edu> coughed up the following: > On Thu, 1 Jul 2004, Thomas G. Marshall wrote: >> >> Aside: I've looked repeatedly in google and for some reason cannot >>...
102
by: junky_fellow | last post by:
Can 0x0 be a valid virtual address in the address space of an application ? If it is valid, then the location pointed by a NULL pointer is also valid and application should not receive "SIGSEGV"...
22
by: Christopher Benson-Manica | last post by:
Is adding 0 to a pointer to non-void that is equal to NULL legal? int *p=NULL; p+=0; -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org ...
6
by: doncee | last post by:
This is the set up: is a date field on a form. Its control source is the field "date_entered" in"Table1". If there is a date showing in the date_entered field (Text6) & the status in Combo box2...
31
by: leonm54 | last post by:
I remember that this is a bad practice, but can not find a definitive resource that states it is bad and why. Can anyone help?
28
by: hijkl | last post by:
hey guys anything wrong with this code?? if it is then what? int *array(int n){ return new int(n); } int main(){ int *p = array(10); for( int i = 0; i < 10; i++ ) {
30
by: Bill Reid | last post by:
#define MAX_VALUES 64 typedef struct { unsigned value_1; double value_2; double value_3; double value_4; } VALUES; typedef struct {
5
by: Ark Khasin | last post by:
Consider, at file scope, struct {void *foo, *bar;} X = {.foo=NULL,}; bar will get all-bits-zero which AFAIK officially ain't necessarily a NULL. Now, how bad is the assumption that X.bar==NULL?...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.