Looks like dereferencing a null pointer, but is ok???

Georg Wrede georg.wrede at nospam.org
Tue Sep 12 04:26:18 PDT 2006


nobody wrote:
>>>> void main()
>>>> {
>>>>     char[] foo = (cast(char*)null)[0..10];
>>>> }
>>
>> I would have expected this to cause an error!
>>
>> And if really not, what in the world is
>>
>>    (cast(char*)null)[0..10]
>>
>> supposed to mean, or return???
> 
> It is an array of the first 10 char elements starting from memory 
> address 0.

In the ways not otherwise specified, D is alike C and C++, at least in 
spirit.

Bjarne says (C++, 3rd ed. p 5.1.1) "No object is allocated with the 
address 0. Consequently, 0 acts as a pointer literal, indicating that a 
pointer doesn't refer to an object." ((I take it this means not only 
class object instances but anything pointable at all.))

For some reason I got no compile time nor runtime error from this.

We're using the slice as an rvalue, so this constitutes a dereference of 
a null pointer. And therefore an error.

(I seem to remember having read somewhere that modern systems don't give 
user programs address spaces containing the zero address (even 
virtually), precisely to avoid problems and pitfalls with null. Can 
anybody confirm this?)

Further, since NULL is explicitly defined to not point to anything 
(therefore not even the address zero), at best this code attempts to 
return a slice out of thin air. In other words, garbage.

And specifically, even if there /was/ a zero address to point to, it 
would still be illegal to return the slice.

---

The following program does die (in windows) with an Access Violation:

import std.stdio;

void main()
{
         char[] foo = (cast(char*)null)[0..10];

	char c = foo[1];
}

And we weren't even accessing foo[0]. :-)

---

I consider it a bug when the compiler creates such an unusable array.
I consider it an bug to not error on (cast(char*)null)[0..10].



More information about the Digitalmars-d-learn mailing list