Path: utzoo!utgpu!water!watmath!clyde!bellcore!faline!thumper!ulysses!ucbvax!bloom-beacon!mcgill-vision!mouse
From: mouse@mcgill-vision.UUCP (der Mouse)
Newsgroups: comp.sources.d
Subject: Re: Simulation of 'alloca' -- can it be done in TurboC or MSC?
Keywords: alloca
Message-ID: <1131@mcgill-vision.UUCP>
Date: 3 Jun 88 03:41:11 GMT
References: <203@lcuxa.UUCP> <443@happym.UUCP>
Organization: McGill University, Montreal
Lines: 35
Posted: Thu Jun  2 23:41:11 1988

In article <443@happym.UUCP>, kent@happym.UUCP (Kent Forschmiedt) writes:
> I don't know about TurboC, but MSC seems to use a conventional stack
> structure.  The alloca() that I use actually uses malloc() and
> free(), but saves a tag pointing to the current stack frame whenever
> a block is alloca()'d.  When alloca() is called, it free()'s all of
> the blocks whose tags point below (on a down-growing stack) the
> current frame.  I think that this scheme will work with any
> implementation that uses a real stack, [...]

I doubt it.  How about

main()
{
 while (1)
  { test();
  }
}

test()
{
 char *foo;
 char *alloca();

 foo = alloca(10);
}

With a proper alloca() (free on return), this will be an infinite loop.
With the scheme you suggest, it will slowly chew up memory, ten bytes
at a crack.  Unless you have some scheme for putting serial numbers in
stack frames, so you can tell one call to test() from the next....

					der Mouse

			uucp: mouse@mcgill-vision.uucp
			arpa: mouse@larry.mcrcim.mcgill.edu