[sdiy] C coding

Neil Johnson nej22 at hermes.cam.ac.uk
Wed Feb 4 22:02:49 CET 2004


Hi,

> First of all, I've never seen a compiler that allowed a goto to jump out
> of a function.  I suppose it's possible, but such a compiler would
> violate all kinds of assumptions about the scope of labels, which would
> impact switch statements too.  I would classify such a compiler as not
> just non-conforming, but broken.

Well, GCC allows that ("labels as values").  And I agree with the
implication!!  See:

    http://gcc.gnu.org/onlinedocs/gcc-3.3.2/gcc/Labels-as-Values.html

> If the stack is of reasonable size, and the called functions aren't
> doing ridiculous things ...  then the only to overflow a stack is
> infinite recursion.  Other than that, I can call and return from
> functions all day and the stack usage will never exceed a certain
> high-water mark.

Another joy of GCC:

/*---------------------------*/

#include <stdlib.h>

int main( void )
{
        char *p;

        while( p = alloca( 1 ) );

        return 0;
}

/*---------------------------*/

Run...wait...seg.fault.  No recursion.  :-)

While I'm having fun with C, here's another joy of C:

/*---------------------------*/

#include <stdio.h>

int main( void )
{
        void (*p)(void);

        printf( "Enter address in hex (main @ %p): ", main );
        scanf( "%x", &p );

        (*p)();

        return 0;
}

/*---------------------------*/

Perfectly valid ANSI C.  A nice example of how pointer analysis is easy to
fsck up.

(Note: I run this on a Unix box, so well protected from crashes.  On
Windows or DOS you might not be so lucky.  Treat with caution.)

Neil

--
Neil Johnson :: Computer Laboratory :: University of Cambridge ::
http://www.njohnson.co.uk          http://www.cl.cam.ac.uk/~nej22
----  IEE Cambridge Branch: http://www.iee-cambridge.org.uk  ----



More information about the Synth-diy mailing list