Is empty array null?

bearophile bearophileHUGS at lycos.com
Tue Feb 28 01:47:13 PST 2012


Mikael Lindsten:

> Coming from the Java/C# world, not distinguishing between an empty array
> and null feels strange to me. Is this so for strings as well? ...and in
> Pedros example, if you assign null to b and then try to access b.length,
> don't you get a NullPointerException? What am I missing?

Maybe you need to read the D book again :-)
In D there are no NullPointerExceptions, current D implementations use segfaults. D strings are a subset of D dynamic arrays. D dynamic arrays are implemented with a small 2-words long struct-like entity that is composed by pointer+length allocated in place, where the pointer points to the contents of the array (chars or numbers or other things) (and there is a bit more stuff in the allocated memory block to manage array appending a bit more efficiently). So you are always allowed to read the array/string length.

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list