groff
[Top][All Lists]
Advanced

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

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


From: G. Branden Robinson
Subject: Re: Counterexamples in C programming and library documentation (was: [PATCH v3] NULL.3const: Add documentation for NULL)
Date: Wed, 3 Aug 2022 10:58:31 -0500

Hi Alex,

I'll reply to one bit of this for now and the rest later.  I need to eat
my vegetables (fix that groff man(7) `CHECKSTYLE` warning)--I've got it
down to 2 test case failures.  At least four of my tests were adequately
irascible to find real problems!

At 2022-08-03T01:54:18+0200, Alejandro Colomar wrote:
> On 8/2/22 21:06, G. Branden Robinson wrote:
> >   I don't think it is an accident that there are no function pointer
> >   literals in the language either (you can of course construct one
> >   through casting, a delightfully evil bit of business).
> 
> 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.

  (*(void(*)())0)();

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.

https://www.bell-labs.com/usr/dmr/www/odd.html

Regards,
Branden

[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.

      https://lwn.net/Articles/342330/

    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.

Attachment: signature.asc
Description: PGP signature


reply via email to

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