5 ms·
Why is int in C in practice at least a 32 bit?
- deleted 3y ago[deleted]
- userbinator 3y agoBecause 32/32 architectures (32-bit registers, 32-bit address space) became dominant for a long time. I'm referring to the 386, 68k, and the common 32-bit ARMs. I still remember the era when int was usually 16 bits and you needed a long to get 32. This is still true for smaller embedded systems.
- SV_BubbleTime 3y agoPIC micros man, int was 8bits for me like… 13 years ago.
- jcranmer 3y agoC does require that int is at least 16 bits (e.g., INT_MAX has to be at least 32767).
- SAI_Peregrinus 3y agoISO C requires that. PIC C doesn't. There aren't that many C dialects, but there are a few.
- Findecanor 3y agoYou can often override that to be 8-bit on compilers for 8-bit microcontrollers, on which C's integer promotion rules to 16 bits can be costly. I'd think that while a good compiler should be able to optimise those away, even a good programmer might not always write code that provides the compiler with the hints to do that.
- kristopolous 3y agomost arduino is 16-bit int, right now, except when it's not. good luck.
- ack_complete 3y agoIn the other direction, Lattice C used 32-bit ints on the 68000, where 16-bit was more efficient. This led to a bunch of code explicitly using short instead of int, and then later being converted back to int for later platforms -- sometimes with amusing results, like a comment labeling an error code as meaning "the buffer is too int."
- cozzyd 3y agoSometimes you need double buffering
- QwertyPi 3y ago[flagged]
- mjevans 3y agoA related big C myth that I believed for far too long relates to structs / bitfields and how they are NOT required to be packed as tightly as possible by the spec. Worse, offhand, I don't know of any portable / standard way to achieve that. Only compiler specific extensions. (I can hope it might be portable to E.G. clang) https://stackoverflow.com/questions/11770451/what-is-the-meaning-of-attribute-packed-aligned4 https://stackoverflow.com/questions/11770451/what-is-the-mea... -- struct __attribute__((packed, aligned(4)))
- jeffbee 3y agoWhich way did the myth go? I am unclear on your statement. In my experience, which admittedly does not include all members of our industry, most people are using `packed` in the same way that Gentoo users were using `-funroll-loops`. In fact, less than 2 hours ago I had to inform my worst colleague (all time career worst) that `aligned(N)` means AT LEAST N, not exactly N. Same colleague also goes along with widely-believed error that `assume_aligned` somehow makes it safe to do incorrectly-aligned accesses, when what it really does is causes the compiler to continue instead of aborting when it sees your code which is obviously wrong.
- uticus 3y ago> I don't know of any portable / standard way to achieve that... Don't you simply make sure all bit fields are adjacent? https://beej.us/guide/bgc/html/split-wide/structs-ii-more-fun-with-structs.html#bit-fields https://beej.us/guide/bgc/html/split-wide/structs-ii-more-fu...
- kevin_thibedeau 3y agoThis mindset leads to a multitude of bugs and brittle non-portable code. Almost as bad as sticking pointers into a uint32_t and breaking portability off of 32-bit platforms. int is 16-bits minimum. If 2^15-1 isn't enough range, switch to long and stop writing brittle code.
- pornel 3y agoI call a bluff on that. 64KB is small enough that it's not just yet another target. It requires programming in a completely different way, one that is unacceptably ugly and contorted for 32-bit and larger targets. If you're writing code and not specifically targetting 64KB systems, your code will be completely unusable on such systems anyway. Most programs and libraries written for larger platforms will have more code than a 16-bit target can even address. Even if you use theoretically correct sizes, they'd still be inappropriate. 16-bit lengths are a bloat if you could fit data in 8 bits, and you'd try hard to do so. There's hardly any room for stack, so even function arguments and local variables are a luxury to be avoided. Real 16-bit programs are often just a one huge function and all global variables.
- ndiddy 3y agoThere are platforms with 16-bit int and a larger than 16-bit address bus, such as AVR and Amiga. > Real 16-bit programs are often just a one huge function and all global variables. I've written software for extremely resource-constrained microcontrollers (7KB flash, 256 bytes of RAM) in "normal C" with many functions, code organized in multiple files, etc. In one case it was to replace firmware written by someone with your mindset, and the resulting code was smaller while having more features.
- kristopolous 3y agoYou should really be using stdint.h types these days unless you are just writing personal stuff/toys/doing homework. Do it even in using temp variables for things like for loops. Keep things explicit, distinct and obvious. It will help you debug later on. * The inttypes.h manpage has a rationale section that's important here. Especially in data structures if you are hitting the disk or network - the data structure alignment can get screwed up once it hits another machine. It's not worth it; stable reliable C is hard enough already. Things tend to break in stupid ways when you do things in old school ways. It's why people replaced that stuff. * - As an example, instead of calling the collection of a type, say "review" as say, "reviews", call it something like "reviewList" or "reviewGroup" - it's easier to scan for errors and you keep the type with the name. This pays off big time especially in larger, messier code-bases where there's multiple ways to do collections of things that only differ in dumb bug-ridden ways. You're only going to work on broken code because code that's not broken doesn't need work.
- cperciva 3y agoEspecially in data structures if you are hitting the disk or network - the data structure alignment can get screwed up once it hits another machine. The only integer type you should be using in data structures which are going out to disk or network is uint8_t. Otherwise the uint32_t you wrote to disk on an x86 system might end up getting read on a PDP-11 and suddenly your little-endian value is middle-endian instead. (I would love to see the C standard grow explicitly-endian types so you could write e.g. "ubeint32_t x = 0x12345678;" and have the value serialized in big-endian form. But alas the C standard moves slowly.)
- kristopolous 3y agoIn practice I just hton[sl] it for both - it's the great equalizer and will get optimized out when not needed at even modest -O levels. If I'm going to crack open an int32, it's htonl all day long and ntohl it back when I'm done.
- chongli 3y agoWhat does it mean to have an "explicitly-endian" type? In the register, it's all the same thing. I have seen tons of code with #if BIG_ENDIAN ... and it's a huge nightmare. Follow the Rob Pike way [1] and regain sanity. [1] https://commandcenter.blogspot.com/2012/04/byte-order-fallacy.html https://commandcenter.blogspot.com/2012/04/byte-order-fallac...
- chihuahua 3y agoI started using C in the late 80s and have used C++ on and off since then. I've not been able to understand how it ever made sense to have a language where the size of int (and other types) varies depending on the platform and implementation. I don't think I've ever been in a situation where I thought "I need this variable to have a type that's the 3rd smallest available, but I don't care how many bits it actually is. On some platforms, it will overflow at 32768, and on other platforms it will overflow at 2.1 billion, and I'm fine with either behavior." Every time I choose a type for a variable, I'm thinking "32 bits is an appropriate size for this variable, and this other variable needs 64 bits". I understand that these days we use int32_t etc, but why did the unpredictably-sized types ever make sense to anyone?
- smashed 3y agoPerformance and transparency to the underlying system. Int would be whatever the instruction set of the platform supports by default for most of its arithmetic and memory operations. C would be sitting a layer above assembly but not try to hide what is going on behind the scene I guess. Much later on languages like java would bring the concept of virtual machines where the types are unified across different instructions set.
- Zanni 3y agoYou're straw-manning a bit with your "3rd smallest type available." Int was supposed to be the natural word-size for the system, which, despite C being "portable," you had to know for efficiency reasons. Memory access at the time was slow, and if you specified an Int that you couldn't load/unload with a single instruction or fit into a register, you would kill your performance.
- chihuahua 3y agoThanks for the response. On one hand, it makes sense that the natural word size is relevant. On the other hand, that means it encourages writing non-portable code.
- jcranmer 3y ago> Memory access at the time was slow Actually, memory access was fast (assuming you're referring to the time when C was developed, not when ANSI C was standardized), as you could access any individual memory location in a single clock cycle. It's once you start seeing the development of RISC systems that being able to clock processors faster than memory starts becoming a thing, and that makes memory slow. > and if you specified an Int that you couldn't load/unload with a single instruction or fit into a register, you would kill your performance. This is the bigger issue. If you have to do lots of extra masking or adjustment of the values to emulate an N-bit integer that isn't otherwise available on your system, that's the slowdown.
- the__alchemist 3y agoGood timing! I was having a conversation today with someone on a CUDA discord about it. The CUDA examples generally use `int` for the pointer size. I'd assumed it was a shorthand for 32-bit-unsigned integer from the context, but it's apparently a standin that the compiler attempts to do the right thing with. So, it's often used as you'd use `usize` in Rust, but not always! For now, my model is: use `int` where you would use `usize` in rust. It can also be used for other things, but it's better to use `uint32_t` or another explicitly-sized type.
- jcranmer 3y agoThat's a really bad idea. The C equivalent to `usize` is `uintptr_t` or `size_t` [1]. `int` is a 32-bit integer on all common platforms, while `long` is 32-bit on some 64-bit platforms and 64-bit on others. But on 64-bit platforms, `int` is not going to be large enough to store a pointer. [1] There's a subtle distinction between the two of them, but on most practical architectures you're coming across now, size_t == uintptr_t. This is actually a bit of a fight right now in Rust, since there's disagreement on what `usize` should do on systems where size_t != uintptr_t (the most common of which is CHERI).
- the__alchemist 3y agoThank you! Updating my code with this.
- pangguoming 3y agoBecause the data bus of computers is generally 32 bits wide.