user2970477
user2970477

Reputation: 129

Difference between 4-byte types in 32-bit and 64-bit Linux machines

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?

Upvotes: 1

Views: 2015

Answers (3)

user2970477
user2970477

Reputation: 129

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.

Upvotes: 2

Thomas Matthews
Thomas Matthews

Reputation: 57698

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.

Upvotes: 1

user3875690
user3875690

Reputation: 139

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).

Upvotes: 0

Related Questions