Linked by Owen Anderson on Mon 19th Apr 2004 05:43 UTC
General Development For years the development scene has been dominated by the C family of languages, primarily C itself and its immediate successor C++. Recent years have given rise to other C-descendents, however, such as Sun's Java and Microsoft's C#.
Permalink for comment
To read all comments associated with this story, please click here.
RE: Null Pointers
by Marcus Sundman on Thu 22nd Apr 2004 20:11 UTC

> It says that part of its goal is to allow the programmer
> hardware level access and embedded assembly language.

Are you implying that low-level access and higher-level semantics are mutually exclusive? Get a clue!

This is the first sentence under the heading "Strings" in the D specs: "Languages should be good at handling strings." I agree.

> Now we are supposed to ignore the hardware and assembly
> language in a language DESIGNED to use them?

I have said no such thing! You are putting words in my mouth.

> Why should a language keep a bunch of pointers to bytes
> which are always 0?

An empty string should NOT(!) be a pointer to a byte/word/whatever that is 0! This is what I'm trying to tell you.

From the D specs: "strings [...] are not 0 terminated in D"

The pointer to the string data should of course be null for empty strings, but the pointer to the string should NOT. See a few paragraphs below if you're too stupid to understand the difference between 'string' and 'string data'.

> That's all a null string is - a pointer to a byte set to
> 0 (in C).

Argh! There is no such thing as a "null string"! Either the thing it a string or it is null, it cannot be both. You just don't make sense!

Also, a 0-terminated character array is just one particular implementation of a string. Modern programming languages really shouldn't use such a na´ve implementation because of its drawbacks.

> Why not set null strings to the same byte to save space?

Still not making sense. However, if you by "null strings" mean "empty strings" then sure, point them to the same empty string as long as you make sure that resizing one empty string doesn't affect the other.

> Why not set all null objects to the same pointer - a
> special code which means the object is empty...

You are so confused. There is no such thing as a "null object".

> Who CARES if ""=null as long as if (astr == "") works
> properly?

I already wrote a program where it is cruicial that there be a distinction between an empty string and no string at all.

Let me make still another example. Let's say I have a function like 'setPassword(char[] newPassword)'. I want 'setPassword(null)' to mean 'there is no password, and therefore there will be no password requester'. I want 'setPassword("argh")' to mean 'there is a password, namely the string "argh", and there will be a password requester'. Consistently I want 'setPassword("")' to mean 'there is a password, namely the string "", and there will be a password requester'. If ""==null then I'd have to add some other method or parameter to specify whether or not there is a password at all, and it would need a separate boolean variable, separate checks etc. It would work, but it'd be very ugly. I don't want programming languages to enforce ugly implementations.

> Like you said, it's an implementation detail. So why the
> big fuss if someone wants to make it the same as null??

I don't make a big fuss about it, I just think it's as stupid as it would be to make "argh"==42.

> You have yet to give a reason why not

Yes I have, and in this posting I have given you yet another one.

Let me quote a few things from the D specs before your stupidity overwhelms you completely.

In the "Strings" section we find this: "a string is just a dynamic array of characters", and in the section about arrays we find this: "Dynamic arrays contain a length and a garbage collected pointer to the array data." So an array is a pointer to a structure that has at least the attributes 'length' (an integer) and 'data' (a pointer, let's ignore any memory manager structure that might be wrapping it), and possibly some type info or something. An empty string would therefore logically have 'length=0' and 'data=null'. Now, what _I_ want is to be able to distinguish between a pointer to null and a pointer to an empty string, because of the reasons I've given.