Null Pointer
Is const void* p the same as a null pointer? Why would i use it?
bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
Is const void* p the same as a null pointer? Why would i use it?
No, that's a pointer named p. You might use it for holding the memory address of something. For example, memcpy takes a const void* argument.
Rashakil Fol
Super Senior Demiposter
2,658 posts since Jun 2005
Reputation Points: 1,135
Solved Threads: 177
A NULL pointer is a pointer that points to address 0
const void *p = 0;
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Address zero would not be an absolute address, or is it? Otherwise, what does it mean? I am confused now.
bumsfeld
Nearly a Posting Virtuoso
1,445 posts since Jul 2005
Reputation Points: 404
Solved Threads: 184
>A NULL pointer is a pointer that points to address 0
Bad dragon, now you're just confusing people. A null pointer doesn't have to refer to a specific address, and just because 0 is a null pointer constant doesn't mean it points to address 0.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
just because 0 is a null pointer constant doesn't mean it points to address 0.
You are splitting hairs -- the memory location of a pointer contains an address. A NULL pointer ALWAYS has absolute address 0 in C and C++ programs. Its up to the implementation to evaluate that address as appropriate for the operating systems. There are many references to that, here's just one of them, including Bjarne Stroustrup
http://www.eskimo.com/~scs/C-faq/q5.2.html
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Sometimes there is a difference in C, but not in C++. C compilers often define NULL as (void *)0 or (char *)0, but all standard C++ compliant compilers define NULL as 0. For that reason, NULL is never (supposed) to be applied to integers, just pointers.
int n = NULL; <<< WRONG
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
.or does it mean '\0' and 0 are same
Yes, '\0' and 0 are the same.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>There are many references to that, here's just one of them
Neither of those links (which I've seen before) agree with your claim about a null pointer pointing to an absolute address 0. 0 is a null pointer constant but has nothing to do with the value (absolute or not) of a null pointer. It's just that 0 makes more sense than 523 or 'Q', both of which could just as easily be used as a null pointer constant. I'm not splitting hairs because you're encouraging a common and annoying misconception about null pointers. However, if you want to get the best and brightest from comp.lang.c to say that you're right and I'm wrong, I'll be happy to appologize.
>Is there a difference between NULL and '\0'
Technically, in C++, no. They both evaluate to 0. However, mixing them up can be dreadfully confusing to a reader because NULL refers to a pointer context and '\0' refers to a character context. They're not logically interchangeable. In C, you would be wise not to mix them up because NULL is often defined as ((void *)0), which is most certainly not a character type.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
I understand the confusion because I said "A NULL pointer ALWAYS has absolute address 0" which is incorrect. The pointer does not have an address of 0, its the value stored at the pointer's address which is 0. So I apologize for any confusion this may have caused. The term "null pointer" always referrs to the value stored at the pointer's address, which is 0 when NULL.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>The term "null pointer" always referrs to the value stored at the pointer's address, which is 0 when NULL.
That's still not correct as it strongly implies that a null pointer has the value 0, which is all bits zero, which is not necessarily the case. You really should read the rest of the C FAQ that you so kindly linked to earlier in this thread. It covers this issue adequately.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
If 0 is good enough for Bjarne, then it's good enough for me. Its up to the compiler to interpret that 0 for the target operating system -- programmers just need to put a 0 there. The language would not be very portable if the programmer had to worry about whether or not any given implementation did or did not recognize address 0 as a valid address, or even if it had an address 0. That's the compiler's job.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>If 0 is good enough for Bjarne, then it's good enough for me.
0 is good enough because that's how the language works, duh. But saying that a null pointer has a 0 value displays a fundamental confusion between the abstract and the concrete. In the abstract, 0 is a null pointer constant. But in the concrete, you don't know what the actual value of a null pointer is.
>programmers just need to put a 0 there
Correct, but a lot of programmers still want to know how things work under the hood, even if an abstract understanding is enough for you.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
I think I already mentioned it is up to the compiler to interpret null pointers for the destination operating system. So I don't see any differences of opinion. you are making a really trivel distinction that most of us mortals don't need to worry about. True, its not trivel to an embedded programmer, but this thread is not about embedded programming.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343