3 ms·
The article mentions that undefined behavior permits wrapping and trapping implementations. What about bignums? Would a C or C++ implementation be allowed to al
by ramshorns 10y ago
The article mentions that undefined behavior permits wrapping and trapping implementations. What about bignums? Would a C or C++ implementation be allowed to allocate more space to deal with integer overflow?
On the one hand, C data types are supposed to be fixed sizes, but on the other, "integer overflow = undefined behavior = anything can happen" seems to permit extending the size of the type when it overflows.
- TorKlingberg 10y agoI may be late to answer here, but no you couldn't reasonably make int a bignum in C. Unsigned integers are defined to wrap around, so certainly not there. Signed overflow is undefined behaviour so the compiler and runtime can do anything. But, where would it store the extra bits? It could call malloc, but it wouldn't know when to free. It would violate the expectation that memory is managed manually in C. Not all C code even uses malloc. Or, what if the int is in a struct? Should the implementation go allocate extra overflow space elsewhere, and then what happens when you memcpy the struct? I think there is a strong impedance mismatch between bignums and languages that aren't garbage collected.