home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19329 < prev    next >
Encoding:
Internet Message Format  |  1993-01-06  |  2.0 KB

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