5 ms·
The lack of specified type sizes is something that bit me recently. On pretty much every x86-64 C/C++ compiler, sizeof(long) is 8, except on MSVC where it's 4.
by TillE 10y ago
The lack of specified type sizes is something that bit me recently. On pretty much every x86-64 C/C++ compiler, sizeof(long) is 8, except on MSVC where it's 4. Thank goodness for explicitly sized, now-standard types like int64_t.
- tptacek 10y agoThey're standardized now, but they've been in universal use since before I started writing C in 1994.
- Jtsummers 10y agoI wish they were in universal use. I'm constantly cleaning up a colleagues code where he's made many (now incorrect) assumptions about what "int", "short", "long" would mean. Even pointing him to the standard didn't help, I just got back his stock answer, "It works on X so why do we need to worry?" To which I reply, "Because we've changed architectures and compilers since then and now your code is frequently broken." You'd think he'd learn after a while, but some people are too stubborn.
- AnimalMuppet 10y agoI started when sizeof(int) = 2, sizeof(long) = 4. I've seen int go from smaller than a long to equal to a long (both 4 bytes), and then back to not equal (sizeof(long) = 8). And if that colleague won't learn, that sounds like an issue in need of a bit of active management...
- Jtsummers 10y agoUnfortunately he's got the ear of the management. He's a good guy, and actually a good programmer. He's one of those folks that can churn out 1000 lines of (mostly) functioning code in a day. It's not well-written, but it does the job and that's what management wants to see (bad management, but hard to fight). But he's lousy about not learning new things and being stuck in his ways. Once an idea enters his head it's hard to dislodge it. He learned programming from a mediocre CS school (despite the expensive price tag, which they deserve for their medical and law schools, not their engineering disciplines). And when something works, it's hard to convince him that it's wrong (that is, correct by coincidence) or unstable (in this case, sizeof(int) and related issues). He isn't technically (one of his favorite words) wrong in many instances, but problems will arise when we change OS, compiler, or CPU. I'm slowly converting him to seeing things my way. My main contribution has been to mentor the junior developers. Things will change as their contributions worm their way into our projects.
- pjmlp 10y agoI surely don't remember using them while creating code across MS-DOS, Xenix, DG-UX, Windows 3.1, just constraining myself to the compilation targets I used in 1994.
- tptacek 10y agoI didn't do cross-compiled WinAPI / DG-UX in '94, but as luck would have it I did do DG-UX (the first platform I wrote security code on), and I definitely had bit-width types to use.
- pjmlp 10y agoFor Xenix/DG-UX I was only doing standard command line tools, from MS-DOS, and I don't remember seeing any of those. Maybe I was just too ignorant and was limiting myself to what Turbo C had to offer on the MS-DOS side. Of course on Windows we had all those WORD, DWORD, BYTE, BOOL,... macros already, but I was only thinking about actual C standard bit-width types.
- marcosdumay 10y agosizeof(long) >= 4 sizeof(int) >= 2 sizeof(long) >= sizeof(int) That's all you can expect.
- klodolph 10y agoYou literally cannot expect the first two. On word-addressable hardware, you could easily get sizeof(long) = sizeof(int) = 1. This happens on DSPs.
- deleted 10y ago[deleted]
- deleted 10y ago[deleted]
- marcosdumay 10y agoYes. Didn't think about machines where the memory isn't composed of bytes. The best approach is still to not expect anything.
- vortico 10y agoI understand some implementations might break this rule, but what standard states this?
- jcranmer 10y ago(Derived from C11, specifically N1570). §5.2.4.2.1 CHAR_BIT is at least 8 USHRT_MAX is at least 2^16 - 1 UINT_MAX is at least 2^16 - 1 ULONG_MAX is at least 2^32 - 1 ULLONG_MAX is at least 2^64 - 1 §6.2.5p8 guarantees that integers of increasing conversion ranks obeys subset relations, and §6.3.1.1 lays out that short, int, and long have conversion ranks in that order. Thus, sizeof(char) = 1 ≤ sizeof(short) ≤ sizeof(int) ≤ sizeof(long) ≤ sizeof(long long).
- loeg 10y agoAll of these were present in C99 as well.
- krylon 10y agoTo me, stdint.h was quite possibly the best thing about C99.
- peapicker 10y agoIBMi (OS/400) sizeof long is also 4 by default. Not really "64 bit" tho-on IBMi pointers are 16 bytes with teraspace enabled.