Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!lll-winken!uunet!yale!Horne-Scott From: Horne-Scott@cs.yale.edu (Scott Horne) Newsgroups: comp.lang.c Subject: Re: Short code to determine compiler's Message-ID: <66728@yale-celray.yale.UUCP> Date: 18 Jul 89 14:15:40 GMT References: <396@uop.uop.EDU> <225800197@uxe.cso.uiuc.edu> <579@targon.UUCP> Sender: root@yale.UUCP Reply-To: Horne-Scott@cs.yale.edu (Scott Horne) Organization: Yale University Computer Science Dept, New Haven, CT 06520-2158 Lines: 46 In-reply-to: andre@targon.UUCP (andre) In article <579@targon.UUCP>, andre@targon (andre) writes: > > 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. But C doesn't guarantee the order of allocation of registers. How do you know, for example, that n8, n7, ..., n{x} won't be put into registers and n{x-1}..n1 made automatic? Your program depends on the order of allocation and therefore is not reliable. Besides, the rule that the address of a register variable cannot be taken is new in K&R 2; some old compilers might return an ``address''. Nice try, though.... :-) --Scott Scott Horne Hacker-in-Chief, Yale CS Dept Facility horne@cs.Yale.edu ...!{harvard,cmcl2,decvax}!yale!horne Home: 203 789-0877 SnailMail: Box 7196 Yale Station, New Haven, CT 06520 Work: 203 432-1260 Summer residence: 175 Dwight St, New Haven, CT Dare I speak for the amorphous gallimaufry of intellectual thought called Yale?