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.
Null Pointers
by J.F. on Mon 19th Apr 2004 19:26 UTC

The problem is that apparently an empty char array is equal to no array at all. This is clearly a Bad Thing(tm) since there is very much difference between 'str="";' and 'str=null;'.

str=""; means str is a pointer to a single byte containing 0.

str=null; means that str is not pointing to anything at all.

If you look at the compiled code, pointers have to be loaded into a register before they can be used. They often have to have arithmetic performed implicitly as well. During this time, a single check for a zero pointer would easily provide an early-out for the pointer operation. As long as the compiler writer keeps this in mind, using null pointers for empty arrays is BETTER than the alternative. In fact, since you are checking for null pointers as part of looking for empty arrays, you'll automatically catch accidentally null pointers passed in by bad code. Therefore, it's safer to use a null pointer as an empty array.