Path: utzoo!utgpu!water!watmath!atbowler From: atbowler@watmath.waterloo.edu (Alan T. Bowler [SDG]) Newsgroups: comp.std.c Subject: Re: Invalid pointer addresses Keywords: NULL, pointer Message-ID: <21196@watmath.waterloo.edu> Date: 29 Sep 88 23:14:27 GMT References: <12088@steinmetz.ge.com> Reply-To: atbowler@watmath.waterloo.edu (Alan T. Bowler [SDG]) Distribution: na Organization: U. of Waterloo, Ontario Lines: 41 In article <12088@steinmetz.ge.com> davidsen@crdos1.UUCP (bill davidsen) writes: > > I recently proofread a manual which stated that even calculating the >value of an invalid address could cause a memory fault. I read this to >mean that code like: > int D_temp[30], /* 30 elements */ > *tempr = D_temp-100; /* int tempr[100..129] */ > >would cause a problem even if the pointer were not dereferenced. I would >assume that calculating a value would not EVER cause a dereference (and >thereby a fault), no matter how invalid the address. > The problem occurs on those smart architectures that validate a pointer when it is loaded into a pointer type register, or the contents of such a register is changed. In your example assume that you coded x = tempr[i]; You really want the compiler to generate lptr PR,tempr Load pointer register PR ldx IX,i Load index register IX ldr AC,[IX,PR] Load data register AC, using PR indexed by IX sldr AC,x Store data register AC into X If the machine does check the register on the load pointer this could well fault. If the compiler is required to accept a sequence like you want then it will be forced to do the address calculation itself with the data registers and manufacture a pointer before it actually tries to do a data fetch. Since data registers on these architectures are often smaller than the pointer registers, this tends to be a painfully slow proceedure. So you can understand the compiler writer deciding to assume that he will only be given programs that are written in the style the machine was designed for and generating the faster sequence. Often the fast method of doing arithmetic on a pointer involves some form of "compute effective address into pointer register". That way the implementor will not continually be complained at, "that the same program written in Pascal runs 'so much faster' than in C". This does not stop you from writing your code sequence. You just must be aware that there are some machines out there that will not run that particular program. Note that nothing in the above says anything about NULL, since it never actually gets used where the compiler must do an address computation.