home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / std / cplus / 1946 < prev    next >
Encoding:
Internet Message Format  |  1993-01-01  |  1.7 KB

  1. Xref: sparky comp.std.c++:1946 comp.std.c:3311
  2. Path: sparky!uunet!think.com!sdd.hp.com!cs.utexas.edu!rutgers!twwells!bill
  3. From: bill@twwells.com (T. William Wells)
  4. Newsgroups: comp.std.c++,comp.std.c
  5. Subject: Re: Question about sizeof in C/C++.
  6. Message-ID: <C06HBn.Mr6@twwells.com>
  7. Date: 1 Jan 93 13:54:52 GMT
  8. References: <wwJXVB3w164w@amfent.Gwinnett.COM> <1992Dec24.001540.30000@microsoft.com> <jamesc.725854759@bart>
  9. Organization: None, Mt. Laurel, NJ
  10. Lines: 30
  11.  
  12. In article <jamesc.725854759@bart> jamesc@swapsdev.state.state.COM.AU (James Cribb) writes:
  13. : jimad@microsoft.com (Jim Adcock) writes:
  14. : >jamesc@swapsdev.state.state.COM.AU (James Cribb) writes:
  15. : >| Given
  16. : >|     struct Something*  ps;
  17. : >|     unsigned  k;
  18. : >| do the C or C++ standards guarantee that
  19. : >|     &ps[k] == ((char*) ps) + k * sizeof(struct Something)
  20. : >
  21. : >No.  The relationship holds only if ... k is less than or equal
  22. : >to the number of elements in the array pointed-to by ps
  23. :
  24. : My code meets those conditions, but just out of interest,
  25. : if k is too big, what could cause an inequality (of the addresses)?
  26.  
  27. In the compiler I've written, if an object is known to be less
  28. than 64K bytes, its index arithmetic is done with 16 bits but all
  29. pointer arithmetic is done with 32 bits. So, if sizeof(*ps) * k
  30. is over 64K, the pointer arithmetic will create a different
  31. result from the index arithmetic.
  32.  
  33. (Well OK, for this particular example, the compiler is actually
  34. smart enough to see the equivalence between the index expression
  35. and the pointer addition and so the equality would hold. But if
  36. the (char*)ps weren't detectable as an object pointer, they
  37. wouldn't be equal.)
  38.  
  39. ---
  40. Bill                            { rutgers | decwrl | telesci }!twwells!bill
  41. bill@twwells.com
  42.