2

I have just started using c++ and saw that their is a null value for pointers. I am curious as to what this is used for. It seems like it would be pointless to add a pointer to point to nothing.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
ineedalife
  • 97
  • 1
  • 9
  • 12
    What does your [C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) say on the topic? – Algirdas Preidžius Mar 14 '18 at 16:12
  • 4
    It is Tony Hoare's [billion dollar mistake](https://stackoverflow.com/a/5149102/17034). Backgrounder: https://en.wikipedia.org/wiki/Sentinel_value – Hans Passant Mar 14 '18 at 16:15
  • Where does a pointer point before it has something to point to? – tadman Mar 14 '18 at 16:15
  • 1
    @tadman except a null pointer doesn't point to "nothing". It points to absolute address `0`. That's not generally accessible, so it causes a fault. – Charlie Martin Mar 14 '18 at 16:17
  • @CharlieMartin That's an insignificant implementation-detail, though true on many machines. – Deduplicator Mar 14 '18 at 16:19
  • @tadman it points to whereever it was initialised with - or something random – UKMonkey Mar 14 '18 at 16:19
  • 2
    @CharlieMartin Technically speaking, it is UB to dereference a `nullptr`, rather than "causes a fault". – Algirdas Preidžius Mar 14 '18 at 16:19
  • @CharlieMartin It depends on your platform, some don't have any memory protection, but you're right that it's not "nothing", but it's also not "something", as in `nullptr` is a "not assigned" type value. – tadman Mar 14 '18 at 16:20
  • @UKMonkey I was asking rhetorically, as a lot of times people initialize these with `nullptr` until there's an actual value to assign. – tadman Mar 14 '18 at 16:21
  • @tadman For that case there's a std::optional. (But I'm being factious) – UKMonkey Mar 14 '18 at 16:23
  • @UKMonkey That's one of the gifts that C++17 brought to the party, but people survived for decades without it. If you have a class with a pointer you need to initialize that value with something, and if you don't have a target, `nullptr` is better than nothing as later you can test if it's assigned or not instead of not knowing. – tadman Mar 14 '18 at 16:24
  • @tadman survived? still surviving! How much c++ code is out there that's using c++17? 0.1% maybe? Probably still an order or two of magnitudes off! ;) – UKMonkey Mar 14 '18 at 16:26
  • @AlgirdasPreidžius Very technically speaking, deferencing a `nullptr` is well-defined. – Passer By Mar 14 '18 at 16:30
  • 1
    @PasserBy How so? Please provide a section number, of C++ standard, where it is said so. – Algirdas Preidžius Mar 14 '18 at 16:32
  • 1
    @AlgirdasPreidžius I don't have a book I have been watching tutorials and reading through some pdf files. I don't have the money to take a class or anything. – ineedalife Mar 14 '18 at 16:32
  • "I don't have a book I have been watching tutorials". You are doing it wrong. Stop watching tutorials and get a book. – n. m. could be an AI Mar 14 '18 at 16:33
  • 1
    @AlgirdasPreidžius https://stackoverflow.com/a/28483256/4832499 – Passer By Mar 14 '18 at 16:34
  • Googling the exact title of your question might bring up some information. – Jabberwocky Mar 14 '18 at 16:35
  • @PasserBy Ha, nice. Thanks for the link. – HolyBlackCat Mar 14 '18 at 16:38
  • _"It seems like it would be pointless to add a pointer to point to nothing."_ Love it – Lightness Races in Orbit Mar 14 '18 at 16:45

5 Answers5

3

Well, the null pointer value has the remarkable property that, despite it being a well-defined and unique constant value, the exact value depending on machine-architecture and ABI (on most modern ones all-bits-zero, not that it matters), it never points to (or just behind) an object.

This allows it to be used as a reliable error-indicator when a valid pointer is expected (functions might throw an exception or terminate execution instead), as well as a sentinel value, or to mark the absence of something optional.

On many implementations accessing memory through a nullpointer will reliably cause a hardware exception (some even trap on arithmetic), though on many others, especially those without paging and / or segmentation it will not.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • This answer is in fact incorrect. See below, per the standard, the null pointer always has the value 0. – Charlie Martin Mar 15 '18 at 18:45
  • @CharlieMartin: No. It says that a literal 0 (and some related things) is converted to a null ponter value in a pointer context, which is something completely different. The devil is in the details. – Deduplicator Mar 15 '18 at 18:56
1

Generally it's a placeholder. If you just declare a pointer, int *a;, there's no guarantee what is in the pointer when you want to access it. So if your code may or may not set the pointer later, there's no way to tell if the pointer is valid or just pointing to garbage memory. But if you declare it as NULL, such as int *a = NULL; you can then check later to see if the pointer was set, like if(a == NULL).

Most of the time during initialization we assign null value to a pointer so that we can check whether it is still null or a address has been assign to it or not.

Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17
Yserbius
  • 1,375
  • 12
  • 18
  • 4
    In C++ land it's [`nullptr`](http://en.cppreference.com/w/cpp/language/nullptr). – tadman Mar 14 '18 at 16:22
  • @tadman: If you restrict that to C++11+ land, that's true enough, though not quite the full picture. Would have been nicer if they had just bitten the bullet and taken `null`. – Deduplicator Mar 14 '18 at 16:35
  • 1
    @Deduplicator We all have hopes and dreams that end up crushed by a C++ TR. – tadman Mar 14 '18 at 16:37
1

It seems like it would be pointless to add a pointer to point to nothing.

No, it is not. Suppose you have a function returning optional dynamically allocated value. When you want to return "nothing" you return null. The caller can check for null and distinguish between 2 different cases: when the return value is "nothing" and when the return value is some valid usable object.

ks1322
  • 33,961
  • 14
  • 109
  • 164
-1

Basically, pointers are just integers. The null pointer is a pointer with a value of 0. It doesn't strictly point to nothing, it points to absolute address 0, which generally isn't accessible to your program; dereferencing it causes a fault.

It's generally used as a flag value, so that you can, for example, use it to end a loop.

Update:

There seem to be a lot of people confused by this answer, which is, strictly, completely correct. See C11(ISO/IEC 9899:201x) §6.3.2.3 Pointers Section 3:

An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.

So, what's an address? It's a number n where 0 ≤ nmax_address. And how do we represent such a number? Why, it's an integer, just like the standard says.

The C11 standard makes it clear that there's never anything to reference at address 0, because in some old pathologically non-portable code in BSD 4.2, you often saw code like this:

/* DON'T TRY THIS AT HOME */

int
main(){
    char target[100] ;
    char * tp = &target ;
    char * src = "This won't do what you think." ;
    void exit(int);

    while((*tp++ = *src++))
        ;

    exit(0);
}

This is still valid C:

$ gcc -o dumb dumb.c
dumb.c:6:12: warning: incompatible pointer types initializing 'char *' with an
      expression of type 'char (*)[100]' [-Wincompatible-pointer-types]
    char * tp = &target ;
           ^    ~~~~~~~
1 warning generated.
$ 

In 4.2BSD on a VAX, you could get away with that nonsense, because address 0 reliably contained the value 0, so the assignment evaluated to 0, which is of course FALSE.

Now, to demonstrate:

/* Very simple program dereferencing a NULL pointer. */

int
main() {
    int * a_pointer ;
    int a_value ;
    void exit(int);             /* To avoid any #includes */

    a_pointer = ((void*)0);
    a_value = *a_pointer ;

    exit(0);
}

Here's the results:

$ gcc -o null null.c
$ ./null
Segmentation fault: 11
$ 
Charlie Martin
  • 110,348
  • 25
  • 193
  • 263
  • 3
    This answer makes a lot of assumptions about the architecture involved. – tadman Mar 14 '18 at 16:22
  • 1
    That's indeed how it works (more or less), but that's not true from the standard point of view. – HolyBlackCat Mar 14 '18 at 16:27
  • @HolyBlackCat "_That's indeed how it works_" Usually.. Due to the UB nature of such an action (dereferencing a `nullptr`), one can, sometimes, call a method, which doesn't use any member variables, on a `nullptr` `class` pointer. – Algirdas Preidžius Mar 14 '18 at 16:30
  • @n.m. the first sentence is however not _entirely_ wrong. – Jabberwocky Mar 14 '18 at 17:36
  • @n.m. Nonsense. What do *you* think a pointer in C is? An address, right? What's an address? A natural number n with 0 ≤ n ≤ max address. What's a null pointer? it's an address with value 0. I mean, you can look it up: C11(ISO/IEC 9899:201x) §6.3.2.3 Pointers Section 3 – Charlie Martin Mar 15 '18 at 18:12
  • Some architectures may define an address as a natural number, but the C language does not. It says a pointer may be converted to and from an integer, but this doesn't make it an integer. Some architectures may define a null pointer as an address with value 0, but the C language does not. Don't confuse the null pointer constant with the null pointer representation, these are two different things. A null pointer may well be represented with a value of 0xFFFFFFFF at run time. I have seen such platforms. – n. m. could be an AI Mar 15 '18 at 20:44
  • @n.m: Now you're just being silly. Present to me an example of a platform in which machine addresses aren't natural numbers. Similarly, present to me a platform on which, CONTRARY TO THE STANDARD, a null pointer is not represented as value 0. – Charlie Martin Mar 15 '18 at 21:35
  • (1) Ever heard of 8086 based computers? They used to have segmented memory. An address is a pair of integers, a segment and an offset. (2) The standard never says how null should be represented. You are welcome to find a quote to the contrary (the null pointer constant has nothing to do with the representation of null at runtime). See http://c-faq.com/null/machexamp.html. All of this is beaten to death here and elsewhere. – n. m. could be an AI Mar 16 '18 at 05:33
  • @n.m. Sure, segment+offset is not a natural number. You bet, – Charlie Martin Mar 17 '18 at 06:13
-1

null value in C and C++ is equal to 0. But nullptr in C++ is different from it, nullptr is always a pointer type in C++. We assign a null value to a pointer variable for various reason.

  1. To check whether a memory has been allocated to the pointer or not
  2. To neutralize a dangling pointer so that it should not create any side effect
  3. To check whether a return address is a valid address or not etc.

Most of the time during initialization we assign null value to a pointer so that we can check whether it is still null or a address has been assign to it or not.

Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17
  • *"But `nullptr` in C++ is different from it."* This doesn't sound right. Can you elaborate? – HolyBlackCat Mar 14 '18 at 16:29
  • 1
    *"nullptr is always a pointer type in C++"* Actually, the type of `nullptr` is specifically not a pointer type. `*nullptr` is ill-formed. – eerorika Mar 14 '18 at 16:42