Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!mips!apple!ksand From: ksand@Apple.COM (Kent Sandvik) Newsgroups: comp.sys.mac.programmer Subject: Re: Huh? What's wrong here... (A Simple Think C question) Message-ID: <52135@apple.Apple.COM> Date: 29 Apr 91 00:37:32 GMT References: <63903@bbn.BBN.COM> <3417@charon.cwi.nl> Organization: Apple Computer Inc., Cupertino, CA Lines: 45 In article <3417@charon.cwi.nl> guido@cwi.nl (Guido van Rossum) writes: >rmitchel@bbn.com (Rob Mitchell) writes: >>[correct diagnosis of earlier problem omitted] >> IMHO, never use "int" when programming Mac. Use either "short" or >> "long" (or better, declare typedefs for Int8, UInt8, etc.) which >> helps when porting to other computer systems. >I take exception with this advice. Portable programs should use 'int' >for most common cases, especially function parameters, except: > - always use long if values > 32767 are possible > - use short for space conservation in structs and arrays, > when values are guaranteed to be <= 32767. > >The reason is that 'int' is likely to generate the most efficient >code, because it represents the natural word size (register size) for >the architecture. This is true, with the exception of the Macintosh programming world, where programmers may or may not move source and header files between MPW and Think environments. Anyway, most of the optimization issues are really oriented towards 32-bit quantities with the M68k architecture (passing on the stack, fetching on the databus and so on). Anyway, I would also recommend to use short and longs in order to avoid any problematic portability problems *within the Mac development world*. TN#200 is kind of out-of-date (and we will update the TN soon!), but one thing that is relevant in this tech note is the issue of widening and narrowing of basic types with the MPW C compiler. Thus char and short are widened to int (long) size before being passed as function arguments (and narrowed back to char or short in the function). So any possible optimization by using short is not present, with the exception of struct handling, and in many cases the structs are also padded to full 32-bit boundaries. Speaking about other nasty type problems concerning portability, character constants are char in C++, and ints in C :-). Regards, Kent Sandvik -- Disclaimer: Private and personal activities on USENET, non-company sponsored