home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!sun4nl!and!jos
- From: jos@and.nl (Jos Horsmeier)
- Newsgroups: comp.lang.c
- Subject: Re: Moving from Pascal to C, Help please!!!!!!
- Message-ID: <4299@dozo.and.nl>
- Date: 6 Jan 93 16:56:14 GMT
- References: <1993Jan5.162504.1680@leland.Stanford.EDU> <4293@dozo.and.nl> <1993Jan6.151028.2055@leland.Stanford.EDU>
- Organization: AND Software BV Rotterdam
- Lines: 39
-
- In article <1993Jan6.151028.2055@leland.Stanford.EDU> dkeisen@leland.Stanford.EDU (Dave Eisen) writes:
- |In article <4293@dozo.and.nl> jos@and.nl (Jos Horsmeier) writes:
-
- |>Then you've just been plain lucky and so were the authors of NRC.
-
- |My understanding was that Numerical recipes in C used the trick:
- |
- |#define Array (array - 1)
- |
- |and then indexed the array as Array[1] to Array[n-1].
- |
- |Yes, array - 1 is illegal. Even when used as Array[1] which
- |is the same as *(array - 1 + 1). But even if a system uses
- |segments in such a way that array - 1 is not a valid address,
- |it is very likely that array - 1 + 1 gives a pointer that
- |is represented exactly the same way as array is. Even if pointer
- |arithmetic is done using only the offset.
-
- That's what I thought so too, 'till someone pointed out to me that
- the pointer (array-1) points _outside_ the object itself (and it
- doesn't point one byte beyond the object, which is an exception
- to the rule.) All pointer arithmetic, using pointers _not_ pointing
- to a valid object cause undefined behavior. Maybe my example where
- segmented memory was used wasn't a good example. But consider an
- architecture where special pointer registers are used and whenever
- such a register is loaded with a new value, a validity check is
- done. Loading a register with a value (array-1) can cause a core
- dump or whatever ...
-
- |Don't take any of this as an endorsement of this "technique". It
- |isn't. I just said that it was not enough to keep me from buying
- |the book. It is not something I would ever do in my own code; I
- |try to stay out of the areas of undefined behavior.
-
- A very sensible strive! ;-)
-
- kind regards,
-
- Jos aka jos@and.nl
-