Null Pointers Interview Questions C C++

Null Pointers Interview Questions C C++
1. What is this infamous null pointer, anyway?
A: For each pointer type, there is a special value — the “null
pointer” — which is distinguishable from all other pointer values
and which is not the address of any object.

2.How do I “get” a null pointer in my programs?
A: A constant 0 in a pointer context is converted into a null pointer
at compile time. A “pointer context” is an initialization,
assignment, or comparison with one side a variable or expression of
pointer type, and (in ANSI standard C) a function argument which
has a prototype in scope declaring a certain parameter as being of
pointer type. In other contexts (function arguments without
prototypes, or in the variable part of variadic function calls) a
constant 0 with an appropriate explicit cast is required.

3.What is NULL and how is it #defined?
A: NULL is simply a preprocessor macro, #defined as 0 (or (void *)0),
which is used (as a stylistic convention, in preference to
unadorned 0’s) to generate null pointers,

4.How should NULL be #defined on a machine which uses a nonzero bit
pattern as the internal representation of a null pointer?

A: The same as any other machine: as 0 (or (void *)0). (The compiler
makes the translation, upon seeing a 0, not the preprocessor.)

5.If NULL were defined as “(char *)0,” wouldn’t that make function
calls which pass an uncast NULL work?

A:Not in general. The problem is that there are machines which use
different internal representations for pointers to different types
of data. A cast is still required to tell the compiler which kind
of null pointer is required, since it may be different from
(char *)0.

6.I use the preprocessor macro “#define Nullptr(type) (type *)0″ to
help me build null pointers of the correct type.

A: This trick, though valid, does not buy much.

7.Is the abbreviated pointer comparison “if(p)” to test for non-null
pointers valid? What if the internal representation for null
pointers is nonzero?

A:The construction “if(p)” works, regardless of the internal
representation of null pointers, because the compiler essentially
rewrites it as “if(p != 0)” and goes on to convert 0 into the
correct null pointer.

8.If “NULL” and “0″ are equivalent, which should I use?
A:Either; the distinction is entirely stylistic.

9.But wouldn’t it be better to use NULL (rather than 0) in case the
value of NULL changes, perhaps on a machine with nonzero null
pointers?

A:No. NULL is, and will always be, 0.

10.I’m confused. NULL is guaranteed to be 0, but the null pointer is
not?

A:A “null pointer” is a language concept whose particular internal
value does not matter. A null pointer is requested in source code
with the character “0″. “NULL” is a preprocessor macro, which is
always #defined as 0 (or (void *)0).

11.Why is there so much confusion surrounding null pointers? Why do
these questions come up so often?

A:The fact that null pointers are represented both in source code,
and internally to most machines, as zero invites unwarranted
assumptions. The use of a preprocessor macro (NULL) suggests that
the value might change later, or on some weird machine.

12.I’m still confused. I just can’t understand all this null pointer
stuff.

A:A simple rule is, “Always use `0′ or `NULL’ for null pointers, and
always cast them when they are used as arguments in function
calls.”

13.Given all the confusion surrounding null pointers, wouldn’t it be
easier simply to require them to be represented internally by
zeroes?

a)What would such a requirement really accomplish?

14.Seriously, have any actual machines really used nonzero null
pointers?

A:Machines manufactured by Prime and by Honeywell-Bull, as well as
Symbolics Lisp Machines, have done so.

No comments: