[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Counterexamples in C programming and library documentation (was: [PA

From: Alejandro Colomar
Subject: Re: Counterexamples in C programming and library documentation (was: [PATCH v3] NULL.3const: Add documentation for NULL)
Date: Thu, 4 Aug 2022 12:52:40 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.1.0

Hi Branden,

On 8/3/22 17:58, G. Branden Robinson wrote:
At 2022-08-03T01:54:18+0200, Alejandro Colomar wrote:

Could you show an example?  I'm curious.

Sure.  Let me quote Andrew Koenig, _C Traps and Pitfalls_,
Addison-Wesley, 1989.

"I once talked to someone who was writing a C program to run stand-alone
in a microprocessor.  When this machine was switched on, the hardware
would call the subroutine whose address was stored in location zero.

In order to simulate turning power on, we had to devise a C statement
that would call this subroutine explicitly.  After some thought, we came
up with the following.


That piece of code is beautiful.

Expressions like these strike terror into the hearts of C programmers."

I, too, initially viewed it with dismay.  But I stared at it for a
while, and once I parsed it, I cackled with pleasure.[1]

However, my pleasure would turn to anger if some jackanapes stuck a
comment above it reading as follows.

   /* You are not expected to understand this. */


This comment from the V6 Unix kernel is famous, more famous
unfortunately than the fact that Dennis Ritchie himself repented of this
comment, and explained himself beautifully.


[1] Here, at least, the addressing mode of the resulting instruction
     _is_ more or less visible in the C statement.  In this instance I
     don't think "NULL" would make it clearer, and I would come down on
     the McIlroy side of the argument.  But this circumstance has nearly
     vanished from the domain of practical application.  Thanks largely
     to C and the generations of programmers it has fascinated with its
     charming and attractive footguns, null pointers have been chased
     with such neurotic compulsion that it is common these days to
     program the MMU to raise an invalid address fault on any access to
     the first page of memory.  Although I guess as recently as 2009,
     Linux didn't do this.

     On 64-bit architectures where the backing of the address space by
     actual storage is once again sparse, I don't know of a reason to use
     the page at the bottom of memory for storage.

I think this is one of those cases where the all-0s bit pattern doesn't necessarily correspond to a NULL pointer. That's using a pointer to something that exists and can be dereferenced, and happens to be at address 0. So, I agree with you that using the address 0 in this case makes much more sense than dereferencing the NULL pointer, which is supposed to be an invalid pointer, one that doesn't point to anything.



Alejandro Colomar

Attachment: OpenPGP_signature
Description: OpenPGP digital signature

reply via email to

[Prev in Thread] Current Thread [Next in Thread]