[sdiy] C coding

Cornutt, David K david.k.cornutt at boeing.com
Wed Feb 4 22:56:40 CET 2004



> -----Original Message-----
> Another joy of GCC:
> 
> /*---------------------------*/
> 
> #include <stdlib.h>
> 
> int main( void )
> {
>         char *p;
> 
>         while( p = alloca( 1 ) );
> 
>         return 0;
> }
> 
> /*---------------------------*/
> 
> Run...wait...seg.fault.  No recursion.  :-)

I'm surprised to find that that function is even in
GCC.  It's an artifact from the early Berkeley compilers.
Alloca allocates directly from the stack, which is why
it blows up.  True, it isn't recursion, but it does
the same thing to the stack.  And alloca isn't an
ANSI-recognized fuction.  You're supposed to use malloc
in an ANSI-compliant application.


> 
> 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;
> }
> 
> /*---------------------------*/
> 

Well yeah.  C isn't designed to be idiot-proof, which
I think was the point of Magnus's original post.  Of course,
an adb-level debugger will most likely contain code that 
looks like this.



More information about the Synth-diy mailing list