Re: why does this work ?

From: Sidney Cadot (sidney_at_jigsaw.nl)
Date: 10/11/03


Date: Sat, 11 Oct 2003 12:53:07 +0200

Joona I Palaste wrote:

> <snip>
> There is no requirement for implementations to *have* a stack in the first place.

This is the second time I see this posted over the last couple of days,
and you're surely right. But it does beg the following question:

----
#include <stdio.h>
/* returns n! modulo 2^(number of bits in an unsigned long) */
unsigned long f(unsigned long n)
{
   return (n==0) ? 1 : f(n-1)*n;
}
int main(void)
{
   unsigned long z;
   for(z=1;z!=0;z*=2)
   {
     printf("%lu %lu\n", z, f(z));
     fflush(stdout);
   }
   return 0;
}
----
As far as I can see, this is a perfectly valid C program that should 
reach the 'return 0' statement always, were it not for the fact that in 
all compilers I tried (one, actually) it terminates with a segmentation 
fault of some sort, due to limited stack size.
Is this 'incorrect behavior' of all these compilers, or is there some 
wording in the Standard that covers for machines with a finite stack 
(much to my dismay, this covers all machines I have access to)?
If so, is there a minimum depth of function calls that I can rely on to 
be executed properly? I would hate to rewrite all my programs to do 
everything within main() without function calls, for the ultimate 
portability :-)
Best regards,
   Sidney


Relevant Pages

  • Re: why does this work ?
    ... > int main ... > all compilers I tried it terminates with a segmentation ... due to limited stack size. ... > wording in the Standard that covers for machines with a finite stack ...
    (comp.lang.c)
  • Re: how to declare doubles in f95
    ... that's not what I was referring to. ... you seem to be referring to machines instead of compilers. ... find that they have 64-bit default reals, ...
    (comp.lang.fortran)
  • Re: A function is an address
    ... of today's compilers. ... but this is due to the language ... actually calling it) normally produces a pointer to that function. ... on real machines is, in general, the address of the first instruction ...
    (comp.lang.c)
  • Re: controversial paper
    ... >> aren't those machines any better than Pentium machines? ... X86 compilers having to deal with too few registers and a ... fairly arcane instruction set (not to mention the rather strage ... also -- I've not checked recently) compilers did not even use ...
    (sci.crypt)