Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!uunet!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!cbnewsh!warren From: warren@cbnewsh.att.com (warren.a.montgomery) Newsgroups: comp.lang.c++ Subject: Re: 64 bit architectures and C/C++ Summary: More portability problems. How should integer types be specified for maximum portability? Message-ID: <1991Apr29.140256.27605@cbnewsh.att.com> Date: 29 Apr 91 14:02:56 GMT References: <168@shasta.Stanford.EDU> <224@tdatirv.UUCP> Organization: AT&T Bell Laboratories Lines: 51 There are probably a lot of hidden assumptions in programs that sizeof(short)==2 and sizeof(long)==4 at this point, so any assignment of meanings on a 64 bit machine (unless you invent type "long long" for 64 bits and leave long and short at 32 and 16) will cause some pain. All this raises questions about C/C++'s philosophy of integer type specification, which no doubt dates from the days where all machines had only 2 and 4 byte integers. C is unusual in my experience at least in allowing the programmer explicit control over how large integer variables will be, but not tying the specification down to a specific size or value. In PL/1, for example, you explicitly state how many bits you want in a binary integer. This gives you much more predictability than "short" and "long", which are more like hints to the compiler, but I suspect that the extra precision frequently gets in the way. (Both the programmer and the machine wind up doing extra work when an integer that needs to be big enough to hold 1,000,000 gets declared to be 36 bits on a 36 bit machine (simply because the programmer knew that was a magic number, and then ported to a 32 bit machine.) I don't even know if it is possible in C to write a declaration guaranteed to produce an integer of a specific size (in bits or bytes) in a machine-independent way. There are lots of ways a programmer may want to declare an integer: 1 Any convenient and reasonable size. 2 Any convenient size large enough to represent X. 3 The smallest convenient size large enough to represent X. 4 Exactly Y bits or bytes long. 5 Exactly the same size as datatype Z 1 seems to be the intent of "int". "short", "long", and "char" can be used to accomplish 4 for certain values of Y (which differ on each machine). The number of functions written to manipulate 24 bit integers, though, is evidence that this is an imperfect solution. 2,3, and 5 aren't directly expressable in C, but people frequently mean this and use hidden assumptions about how big short and long are in order to do it. There are probably other ways that would be useful to define the size of some integer type, but these will do for a start. How are things like this best expressed in C or C++? Do other languages provide better overall solutions? -- Warren Montgomery att!ihlpf!warren