Re:Interviewing programmers: coding test example explained
by cperciva on Thursday June 03, 2010 @03:23PM
(#1402227)
Whoops, I forgot the point I was trying to make. NULL is (void * )(0), but the code
isn't necessarily valid, since the binary representation of NULL isn't necessarily as a sequence of zeroes -- even though everybody writes code which assumes that it is.
[ Parent
| on HN ]
Re:Interviewing programmers: coding test example explained
by avar on Thursday June 03, 2010 @05:26PM
(#1402627)
Yeah, that's unportable. Although I'm not aware of any system where it
doesn't work.
For the record, regarding 0 in pointer context the C standard has this to say:
An integral constant expression with the value 0, or such an
expression cast to type void * , is called a null pointer constant. If
a null pointer constant is assigned to or compared for equality to a
pointer, the constant is converted to a pointer of that type. Such a
pointer, called a null pointer, is guaranteed to compare unequal to a
pointer to any object or function.
-- C89/C90 section 3.2.2.3 Pointers
The standard then goes on to say that NULL is a macro that's
guaranteed to expand to a null pointer constant.
Re:Interviewing programmers: coding test example explained
by cperciva on Thursday June 03, 2010 @06:10PM
(#1402774)
Yeah, that's unportable. Although I'm not aware of any system where it doesn't work.
Right. I was considering changing this in FreeBSD last year in order to prevent map-at-NULL kernel exploits, but cooler heads prevailed (and pointed out to me how much code such a change would break).
Re:Interviewing programmers: coding test example explained
by nuxi on Friday June 04, 2010 @08:13AM
(#1404066)
Here's the historical list of machines using nonzero null pointers, from comp.lang.c FAQ:
http://linuxdude.com/Steve_Sumit/C-faq/q5.17.html