Jump to content
  • Advertisement
Sign in to follow this  
TEUTON

Null Pointers

This topic is 4577 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Someone asked me What are Null pointers. Initially I got uncomfortable and then answered Null pointers are basically pointers which are being initilised to NULL. Then the next question came Why do you need a NULL pointer. And I was blank and the person moved on with other question(without giving me much time to answer) So plz care to share yur views?

Share this post


Link to post
Share on other sites
Advertisement
Null pointers are used to initialize pointers with a value saying "this pointer actually does not point to a memory block". Hence you could distinct the case "could be dereferenced" and "could not be dereferenced". Typical use cases are lazy pointers (say pointers that are set later at runtime), of course pointers that could be cleared later on, and return values saying that something has gone wrong.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
A pointer should always point to a memory adres that contains a block of memory that the pointer type is representing. A loose pointer points to a random memory adress that don't even have to be same type of data that the pointer represents. Using such a loose pointer will produce unpredictable results. Therefore to be able to check that the pointer don't point to any data you can initialize it to 'null' or '0' or even '0x0'. By always setting an unused pointer to zero you can always make safety checks by comparing with zero. You can think of the zero as a dedicated adress 0x0 that will never point to any data.

Share this post


Link to post
Share on other sites
Another possible answer: pointers are variables. You want to init variables because if you don't, you can't predict their value. If you can't predict their value, you can't use them. Pointers are not different. If you don't init a pointer , you can't know how to use it.

There is two possible ways to init a pointer: either you make it point to a known memory location or you init it with a convenient value that will tell you that it don't point anywhere. NULL appear to be avery convenient value, that's why it is widely used.

Now, from an implementation POV, the C++ Standard Library (CSL) is using new at least once, in a very important place: the delete operator. Deleting a NULL pointer does nothing, why deleting any non-NULL pointer will execute a bunch of operations.

Regards,

Share this post


Link to post
Share on other sites
My take on it would be:

A pointer is simply an offset value into memory, held as a variable.

A null pointer is such a variable that has been set to an agreed invalid value.

You would need a null pointer in any situation where you may need to know that the address being pointed to is not valid, for example deletion or calculating a further offset.

Zero is typically used because it's equivalent to boolean false.


Share this post


Link to post
Share on other sites
Pointers are variables which can "point" to another.

The pointing is optional though. A pointer which doesn't point is called a NULL pointer.

A useful check on a pointer is to check to see if it's NULL before performing actions on it.

Employee* emp;
...
if (emp != NULL)
{
emp->RaiseSalary();
}


Without that check, if
emp
turned out to be NULL, it would crash your program quite unpleasantly.

Sometimes with arrays (especially in Java), you will declare an array of pointers instead of a simple array of objects. This will usually take less memory and gives you the option of leaving unused slots in the array as NULL. Thus, a similar check for NULL would be the same as asking "is this slot in the array empty?"

Share this post


Link to post
Share on other sites
Pointer initialization is essential because a pointer allows access to any memory addresses - valid or invalid.

Kuphryn

Share this post


Link to post
Share on other sites
Quote:
Original post by _winterdyne_
[...]Zero is typically used because it's equivalent to boolean false.
A NULL Pointer is not equal to zero (though the == operator would return true due to definition) and is equal to false only by definition.

The standard could easily be changed to make the upper bound of intptr_t be NULL(a special constant that, when converted to a pointer type, creates a null pointer value) without changing much else, and null pointers would still compare equal to false.

A null pointer's value is undefined. While 0 converted to an pointer type will create a null pointer value, a null pointer value converted to any integer type can have any value.
In other words, int((char *)NULL) == 0 could return true or false and would be standards compliant (at least with the C standard, haven't read the C++ standard) either way.

Share this post


Link to post
Share on other sites
It would be interesting to see how well some programs work if the following code was added:
#ifdef NULL
#undef NULL
#define NULL (~0)
#endif
Places that do:
int *x = NULL;

...

if (x != NULL) { /* blah */ }
would be fine (one reason I preferred this way for so long). But places like:
int *y = NULL;

...

if (y) { /* blah */ }
would not work.
Also places where you interacted with existing code, such as APIs would not always work.

However, it makes a good point about how NULL is simply an agreed value to identify when a pointer specifically does not point to anything. 0 is simply the most convenient value to use as it tends to result in shorter assembly code.

I'm guessing you didn't get the job TEUTON? (It seems pretty likely that this was from a job interview)

Share this post


Link to post
Share on other sites
Quote:
Original post by iMalc
I'm guessing you didn't get the job TEUTON? (It seems pretty likely that this was from a job interview)

There was no need to say that. Salting on my wounds :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!