Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!mcvax!ukc!acorn!moncam!paul From: paul@moncam.co.uk (Paul Hudson) Newsgroups: comp.lang.c Subject: Re: Short code to determine compiler's Message-ID: Date: 19 Jul 89 10:31:45 GMT References: <396@uop.uop.EDU> <225800197@uxe.cso.uiuc.edu> <579@targon.UUCP> Sender: paul@moncam.co.uk Organization: Monotype ADG, Cambridge, UK. Lines: 54 In-reply-to: andre@targon.UUCP's message of 17 Jul 89 16:13:43 GMT In article <579@targon.UUCP> andre@targon.UUCP (andre) writes: In article <225800197@uxe.cso.uiuc.edu> mcdonald@uxe.cso.uiuc.edu writes: > >>Some students here had to determine the number of registers (data >>and address, we use 680x0's) the C compiler uses. A friend and >>I wrote the following code to show to some students having trouble. >>It is very short and simple, but it seems to work. The only logical >>next step is to post it to comp.lang.c and have it torn apart! [ text explaining why this doesn't always work ] >In fact, the really interesting question is, in legal C, is it >even POSSIBLE to write a program to see how many registers are used? Yes, I think you can write a program that checks the nr of registers, but you will get the answer at compile time, not run time :-) the program looks like this: /* test register usage of compiler */ main() { register n1, n2, n3, n4, n5, n6, n7, n8; /* etc. */ int *a; a = &n8; a = &n7; /* repeat n6 - n2 */ a = &n1; } /* end */ The compiler will assing n1 to n{x} to the registers it has available and the rest will be normal variables. You can take the address of a variable but not of a register, so the compiler will start to complain at the first line that tries to take the address of a register. That's why the a = &n{x}; lines must count backwards. I would hope this doesn't work either. I would expect the compiler to complain if I took the address of a variable I had declared register, regardless of whether it was in a register or not. At the time the compiler emits such errors it may not even know whether the variable will get a register or not. -- Paul Hudson MAIL: Monotype ADG, Science Park, Cambridge, CB4 4FQ, UK. PHONE: +44 (223) 420018 EMAIL: paul@moncam.co.uk, ;" FAX: +44 (223) 420911 ...!ukc!acorn!moncam!paul `"";";" These opinions void where prohibited by law.