home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c:12855 comp.programming:2498
- Path: sparky!uunet!mcsun!sunic!dkuug!diku!juul
- From: juul@diku.dk (Anders Juul Munch)
- Newsgroups: comp.lang.c,comp.programming
- Subject: Re: A LITTLE BRAINTEASER... -- generalized
- Message-ID: <1992Aug25.170209.22466@odin.diku.dk>
- Date: 25 Aug 92 17:02:09 GMT
- References: <aet.713608023@munagin>
- Sender: juul@rimfaxe.diku.dk
- Organization: Department of Computer Science, U of Copenhagen
- Lines: 47
-
- aet@mullian.ee.mu.OZ.AU (bert thompson) writes:
- [...]
- >the rules of the game are you can use anything but data structures, or more
- >specifically you cannot use the heap.
- [...]
- >can we make do using only the stack? do any functions require more?
- >(non-primitive recursive functions like ackermann's -- just guessing. 8^)
-
- >apologies if this question turns out to be really easy.
-
- You are asking if C without heap allocation is Turing equivalent. I think it
- is.
-
- Here's a memory allocator that allocates off the stack...
-
- typedef struct mem { DATA d; struct mem* next; } MEM;
-
- void stackalloc(int cnt,MEM* tail, void (*fn)(MEM*))
- {
- if(cnt == 0)
- fn(tail);
- else
- {
- MEM m;
- m.next = tail;
- stackalloc(cnt-1,&m,fn);
- }
- }
-
- How to write accessor functions
- DATA At(MEM*,int loc);
- and void AtPut(MEM*,int loc,DATA val);
- should be obvious.
-
- When you need n units of storage, you just call
- stackalloc(cnt,NULL,my_memory_consumer);
- my_memory_consumer is a continuation function that uses the memory
- allocated (circumventing the problem that the memory cannot be returned
- directly due to stack unwinding with dangling pointers). Using
- continuations is impractical, but I don't think it's a serious restriction.
- Further arguments to the continutiations could easily be passed through a
- modificed `stackalloc'.
- Ackerman's function is certainly no problem (just a lot of work).
-
- Anders Munch | Department of Computer Science
- juul@diku.dk | University of Copenhagen, Denmark
- "Confused? The thing is to get confused at a higher level" - Jorgen Thorslund
-