home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / os / os2 / programm / 4650 < prev    next >
Encoding:
Internet Message Format  |  1992-09-01  |  5.9 KB

  1. Path: sparky!uunet!dtix!darwin.sura.net!wupost!psuvax1!rutgers!njitgw.njit.edu!hertz.njit.edu!dic5340
  2. From: dic5340@hertz.njit.edu (David Charlap)
  3. Newsgroups: comp.os.os2.programmer
  4. Subject: Re: SHORT, USHORT, and other absurdities
  5. Message-ID: <1992Sep1.225220.20880@njitgw.njit.edu>
  6. Date: 1 Sep 92 22:52:20 GMT
  7. References: <1992Aug24.201535.27997@njitgw.njit.edu> <912@nazgul.UUCP>
  8. Sender: news@njit.edu
  9. Organization: New Jersey Institute of Technology, Newark, N.J.
  10. Lines: 109
  11. Nntp-Posting-Host: hertz.njit.edu
  12.  
  13. In article <912@nazgul.UUCP> bright@nazgul.UUCP (Walter Bright) writes:
  14. >In article <1992Aug24.201535.27997@njitgw.njit.edu> dic5340@hertz.njit.edu (David Charlap) writes:
  15. >/Because the C types: short, long, and int are not fixed in size.  int
  16. >/is simply the machine word, long is double that, and short is
  17. >/(usually) int.  By aliassing, them, your program can assume the size.
  18. >/SHORT is always 2 bytes, LONG is always four.  If you go to a system
  19. >/whose machine word is 4 bytes, then the headers will simply define
  20. >/SHORT as an array of 2 chars, LONG as an int, and there will be a new
  21. >/type for the long.
  22. >
  23. >Perhaps. But that isn't the way it works in practice. SHORT is used as
  24. >if arithmetic is allowed on it, and char[2] can't handle arithmetic
  25. >operations. The PM and Windows header files excessively and inconsistently
  26. >use the typedefs to the point where they are pretty well useless.
  27. >If you mean SHORT to be "exactly 16 bits, no more, no less" then it must
  28. >be documented so, or your users will assume it means "short", and will use
  29. >it such. When lots of code assumes SHORT means "short", then you cannot
  30. >change it to mean anything else. A superior approach is to typedef at a
  31. >higher level of abstraction, as in:
  32. >    typedef int WINDOW_HANDLE;
  33. >Then WINDOW_HANDLE could later be typedef'd to be an int, short, double,
  34. >struct, void*, etc., and if you were consistent then user code only needs
  35. >recompiling. No such luck with SHORT.
  36.  
  37. This is done in most cases.  That's why you have types like HWND, HMQ,
  38. HAB, etc.
  39.  
  40. If you read the documentation on data types (the CP reference in the
  41. 2.0 toolkit or the data types reference in the 1.3 toolkit), you'll
  42. see SHORT defined as "a signed integer on the range -32768 to 32767".
  43. That is the definition.  Anything else is your definition.  All the
  44. types are defined like this.  The typedefs in the headers are for the
  45. compiler, not you.
  46.  
  47. >Your code is not portable between 16 and 32 bit OS/2 anyway. IBM completely
  48. >changed the API.
  49.  
  50. You obviously don't know this at all.  I have successfully recompiled
  51. much 16-bit code with gcc/2.  All I did was change the message
  52. parameter from USHORT to ULONG, and all compiled fine.  Very few of
  53. the 16-bit API's have changed.  Some have added new subfunctions, but
  54. the parameter lists have (mostly) gone unchanged.  There are new API's
  55. with different names for the completely new functions.
  56.  
  57. >/Etc.  In some cases, it's not necessary (although char doesn't have to
  58. >/be one byte - especially in a double-byte-character OS, so CHAR is
  59. >/defines as the character size, and BYTE is a byte), but all base types
  60. >/are redefined to make the code more readable.
  61. >
  62. >A double byte character OS is much more likely to use wchar_t, not char.
  63. >Too much code depends on char being a byte. Windows NT is a unicode OS,
  64. >but a char is still a char. I take issue with your statement that
  65. >SHORT as short makes it more readable, *especially* if somebody redefines
  66. >SHORT to be something other than short!
  67.  
  68. It shouldn't matter to you what SHORT is typedef'ed into.  It is
  69. always "a signed integer with a range of -32768 to 32767".  That's all
  70. you need to know.
  71.  
  72. And code is much more readable if you can immediately tell a type from
  73. a variable by looking at the case.  But that's preference and has no
  74. bearing on the OS.
  75.  
  76. >/Why?  All OS/2 programs use these types.  The point is that short may
  77. >/not be 2 bytes on your hardware.  SHORT will always be.
  78. >
  79. >Well, not my OS/2 programs. And I have the same code running on 16 and
  80. >32 bit OS/2. In fact, I compared the API's of 16 and 32 bit OS/2, and wrote
  81. >my own .h file for it which used things like "int" for sizes that changed.
  82. >The IBM .h files used "SHORT" for the 16 bit version and "LONG" for the
  83. >32 bit one. Ugh. I changed that to "int". No #ifdef's!
  84.  
  85. I hope you always use compilers where int is 16 bits for 1.x code and
  86. is 32 bits for 2.x code.  If you switch compilers to one that has, say
  87. a 16-bit int everywhere, or a 32-bit int everywhere, your code is
  88. going to bomb.
  89.  
  90. >You could still write everything with "far", and if your compiler supported
  91. >"__far" instead, you could:
  92. >    #define far __far
  93. >You haven't gained anything with FAR.
  94.  
  95. But you'd have to do it.  It's already been done for you.  If you want
  96. 16-bit code, you have to type something anyway.  Why not use an
  97. identifier that will be supported on everybody's compiler?
  98.  
  99. >/The OS/2 headers attempt to make code independant of machine word
  100. >/sizes, which C does not do.
  101. >/Oddly enough, they never defined a MESSAGE type - messages still use
  102. >/USHORT or ULONG as their type.
  103. >
  104. >Making code independent of word sizes is best accomplished by typedef'ng
  105. >at the MESSAGE level, not at the SHORT or LONG level. I submit that 90%
  106. >of programs that scrupulously use SHORT and LONG typedefs will have to
  107. >be carefully gone through and adjusted if SHORT/LONG are *ever* set to
  108. >anything other than short/long.
  109.  
  110. IT DOESN'T MATTER!!!!   An API call that has a SHORT parameter needs a
  111. 16-bit signed integer as its parameter.  Who cares if that size
  112. variable is called a short, long, char, or whatever?!?!  All those
  113. sizes are undefined in the ANSI spec, and it is very poor practice to
  114. write code that depends on the size of one.
  115.  
  116.  
  117. -- 
  118.    |)  David Charlap           "TELEPHONE, n.  An Invention of the devil which
  119.   /|_  dic5340@hertz.njit.edu   abrogates some of the advantages of making a
  120.  ((|,)                          disagreeable person keep his distance."
  121.   ~|~                              --- Ambrose Bierce, The Devil's Dictionary
  122.