I am porting our Linux C++ project from 32-bit to 64-bit.
In our 32-bit system, "long" is 4-bytes long. But when we built our project in 64-bit mode, we noticed that "long" is taking 8-bytes. Since we need 4-byte long type, we used "int" and confirmed that it is indeed 4-bytes long. And we were hoping that our code works seamlessly.
But unfortunately no, even though "long" in 32-bit mode and "int" in 64-bit mode are 4-bytes long both, our code is failing.
Could someone please let us know if there is a difference (in layout or something else) between "long" in 32-bit mode and "int" in 64-bit mode? Even though both are 4-bytes long?
If you need byte-length specific data types, look into int32_t
and uint32_t
from cstdint
.
The types are guaranteed to be the given length, signed and unsigned.
Thanks everyone. The issue was something local/specific to our code, where we were making an assumption that pointers are always 4-bytes long. When we were dereferencing them, we were casting them to 4-byte variables and were losing precision. This was causing crashes. Since this is a large codebase developed by many people over many years, there was lot of legacy code with these assumptions, I had to go over all the pointers and modify them. And finally we have ported our code successfully to 64-bit mode.
Take a look at this - Size guarantees in C++ . The built-in types (int , long etc.) satisfy some minimum sizes but they need not be 4 bytes on every system. You can use fixed width types (as mentioned in a comment by dwcanillas).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.