scope of malloc
Richard Tobin
richard at aiai.ed.ac.uk
Thu Nov 15 01:08:37 AEST 1990
In article <14448 at smoke.brl.mil> gwyn at smoke.brl.mil (Doug Gwyn) writes:
>It can't be implemented correctly (except by the compiler turning it into
>malloc()/free() calls that you could have coded yourself) on any system
>that doesn't provide separate frame and stack pointers. Just consider
>the effects of an interrupt.
I assume you mean a system that accesses locals by offsets from the stack
pointer, implements alloca() by using space beyond the stack pointer,
and receives interrupts on the same stack. This indeed cannot work.
However, the frame pointer doesn't have to be a dedicated register.
The compiler can just allocate a register for local references in
functions that call alloca(). (I assume that compilers like gcc which
have an option to not use a frame pointer have the same problem, and
disable this option in functions that call alloca()).
And of course if the compiler can turn it into malloc()/free() calls
*and ensure the free() gets done when a longjmp() occurs (which the
user can't)*, then this would be a reasonable implementation.
>It also cannot be reasonably implemented on
>a system that uses linked stack frames, such as on the IBM System/370.
This might be a problem, but as Richard O'Keefe has pointed out, they
would have to solve it to implement various other languages. A
solution in C (which only has local and global variables, and wouldn't
therefore have to follow the frame chain) might be to implement
alloca() by inserting a frame in the chain.
-- Richard
--
Richard Tobin, JANET: R.Tobin at uk.ac.ed
AI Applications Institute, ARPA: R.Tobin%uk.ac.ed at nsfnet-relay.ac.uk
Edinburgh University. UUCP: ...!ukc!ed.ac.uk!R.Tobin
More information about the Comp.lang.c
mailing list