0

As NULL pointer assigns zero to its value.

  1. When I try to cout a null pointer it throws an exception, why is that? It is not outputting zero.

  2. does the value zero have any address?

Example code :

int main()
{
    int *q = NULL;
    cout << *q;
    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
RaHuL
  • 101
  • 1
  • 12
  • 4
    `cout << *q` prints the value that `q` points to. Dereferencing a null pointer is an undefined behavior and modern os usually invalidate the address 0 to prevent errors. If you want to print where `q` points to, use `cout << q`, and if you want to print the address of `q`, use `cout << &q` – KagurazakaKotori Oct 28 '19 at 05:44
  • 2
    Because you're not sending the pointer to `std::cout`, you're telling your program to send what it *points-to* to `std::cout`. And... what does it point to ? Um... you just set it to `NULL`, so, you're dereferencing a null pointer. That's *undefined behavior* plain and simple. – WhozCraig Oct 28 '19 at 05:51
  • 2
    Please check https://stackoverflow.com/questions/24758882/memory-location-of-null-pointer – Paul Oct 28 '19 at 05:56

1 Answers1

1

The value (i.e., the address) stored in a pointer can be in one of four states:

  1. It can point to an object.
  2. It can point to the location just immediately past the end of an object.
  3. It can be a null pointer, indicating that it is not bound to any object.
  4. It can be invalid; values other than the preceding three are invalid.

It is an error to copy or otherwise try to access the value of an invalid pointer. As when we use an uninitialized variable, this error is one that the compiler is unlikely to detect. The result of accessing an invalid pointer is undefined. Therefore, we must always know whether a given pointer is valid.

Although pointers in cases 2 and 3 are valid, there are limits on what we can do with such pointers. Because these pointers do not point to any object, we may not use them to access the (supposed) object to which the pointer points. If we do attempt to access an object through such pointers, the behavior is undefined.

Nikhil Badyal
  • 1,589
  • 1
  • 9
  • 20
  • @t.niese i didn't get this ? – Nikhil Badyal Oct 28 '19 at 06:01
  • Why do you consider 2 to be a special case? Why do you consider it to be valid? – ROX Oct 28 '19 at 11:50
  • @ROX They are used to detect the end of container like array or vector. It is a special case because in reality they don't point to any object.You can use them like sential during traversing the container.So de-referencing them will give undefined results. And although not being of much use language ,they are allowed by language because they are useful sometimes like when to stop in loop during iteration. [link](https://stackoverflow.com/questions/21850108/what-is-pointer-past-the-end-of-an-object-means) – Nikhil Badyal Oct 28 '19 at 12:08
  • If there's a collection of more than one element then a pointer to the 2nd element could satisfy your case 2 (as t.niese points out) but is not the end of container. If you're meaning is that working with collection.end() is a valid thing to do (as long as you only test for it and don't dereference it) then fair enought, although your answer didn't spell that out. – ROX Oct 28 '19 at 12:42
  • @ROX Why you need 2 elements to statisfy 2 case. Even with 1 element in the container 2nd case holds.And that is not the end of container its "past the end ". And about your second point I clearly said that It's use is limited to check the validity of a loop and should not de de-referenced. If you do so , it'll give run time error or undefined result. – Nikhil Badyal Oct 28 '19 at 13:14
  • @t.niese About your first statement **A point that is pointing to a location immediately of the object. (2.) either points to another object (1.) (in case it is e.g. not the last element in a array or vector) or is an invalid pointer.**. It is a valid pointer language doesn't say it is invalid.Just think of this pointer analogous to vector::end() of vector . Is this end() invalid ? It's not , in same way case 2nd pointer is valid. – Nikhil Badyal Oct 28 '19 at 13:18
  • Yes, as you say, case 2 would hold if you attempted to point at element 2 of a single element container, but with a 2 element contigous container a pointer to the 2nd element falls into both your categories 1 and 2. 2nd part I just meant that your answer itself (rather than the follow-up comments) doesnt mention loops or end() of containers. – ROX Oct 28 '19 at 14:29