home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 16967 < prev    next >
Encoding:
Internet Message Format  |  1992-11-21  |  3.5 KB

  1. Path: sparky!uunet!zaphod.mps.ohio-state.edu!cs.utexas.edu!ut-emx!jamshid
  2. From: jamshid@ut-emx.uucp (Jamshid Afshar)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Typedefing builtin... thanks.
  5. Message-ID: <84130@ut-emx.uucp>
  6. Date: 21 Nov 92 23:30:53 GMT
  7. References: <1992Nov9.150414.10221@odin.diku.dk> <1992Nov11.061812.21905@sq.sq.com> <1992Nov13.172005.28239@alf.uib.no>
  8. Reply-To: jamshid@emx.utexas.edu
  9. Organization: The University of Texas at Austin; Austin, Texas
  10. Lines: 58
  11.  
  12. In article <1992Nov13.172005.28239@alf.uib.no> yngvar@imr.no (Yngvar Foelling) writes:
  13. >In article <1992Nov11.061812.21905@sq.sq.com>, msb@sq.sq.com (Mark Brader) writes:
  14. >|>>A program is easier to port, when you can change some typedefs in a header
  15. >|>>file, instead of changing a lot of ints to longs all over the code.
  16. >|> 
  17. >|> If the code was correctly written, those ints would have been longs
  18. >|> in the first place.  See item 10.1 in the FAQ list.
  19. >
  20. >Yes, let's.  Particularly the last paragraph:
  21. >
  22. >        If for some reason you need to declare something with an _exact_
  23. >        size [...], be sure to encapsulate the choice behind an
  24. >        appropriate typedef.
  25. >
  26. >In this case, it seems like Jonatan Kutchinsky _was_ trying to match an
  27. >"externally-imposted storage layout".  He wanted to simulate a particular
  28. >processor.
  29.  
  30. I don't think Mark was commenting on the use of typedefs in that
  31. situation, but on the general advice recommending the use of typedefs
  32. like |int32| instead of |int| or |long|.  Besides, I don't think it's
  33. necessary that the simulator use the same exact number of bits to
  34. represent a byte or word in the simulated processor.  For example, say
  35. the processor to be simulated has 16-bit words.  I would typedef
  36. simword_t as an |int| and would write the code such that it works when
  37. compiled under a machine with either 16-bit or 32-bit ints.
  38.  
  39. I just don't understand how a typedef like int32 is useful in general
  40. applications.  For one thing, you are not guaranteed that there exists
  41. an integral type with exactly that many bits -- imagine an
  42. implementation where all integral types are 64-bits.  Typedefs can
  43. greatly aid readability and encourage implementation hiding, but I
  44. don't think typedefs like int32 buy much.  Why not just use |long|
  45. since it is guaranteed to be at least 32 bits?  At the very least use
  46. <limits.h> and #if's to select the appropriate type instead of
  47. requiring header file changes when porting.
  48.  
  49. >BTW, does anyone know about a C-compiler with 64-bit longs?  Even if there
  50. >aren|t any right now, it seems to me that one would have to be written to take
  51. >advantage of 64-bit architechtures.  In that case, using long instead of int
  52. >would be overkill.
  53.  
  54. Not necessarily.  First, |long| would still be efficient -- they
  55. wouldn't make it 64-bits unless it could handle 64-bits well (not to
  56. mention the machine will probably be several times faster...).
  57. Second, who's to say that |int| and |long| won't be the exact same
  58. size as they are in many current implementations of C?  In fact, these
  59. 64-bit machines will/do probably have |int|==|long| because so much
  60. code that would be ported to them makes the (incorrect) assumption
  61. that they are the same size or that an |int| is the same size as a
  62. |char*|.  Finally, don't forget that the implementation might impose
  63. alignment requirements that would cause the use of one or the other to
  64. only make a difference in very large arrays.  An |int| as a struct
  65. member might just be padded to use the same space as a |long|, or may
  66. require more instructions to be accessed if it were "packed".
  67.  
  68. Jamshid Afshar
  69. jamshid@emx.utexas.edu
  70.