Justin808
Justin808

Reputation: 21512

Is there a difference between datatypes on different bit-size OSes?

I have a C program that I know works on 32-bit systems. On 64-Bit systems (at least mine) it works to a point and then stops. Reading some forums the program may not be 64-bit safe? I assume it has to do with differences of data types between 32-bit and 64-bit systems.

Is a char the same on both? what about int or long or their unsigned variants? Is there any other way a 32-bit program wouldn't be 64-bit safe? If I wanted to verify the application is 64-bit safe, what steps should I take?

Upvotes: 0

Views: 628

Answers (1)

paxdiablo
paxdiablo

Reputation: 881443

Regular data types in C has minimum ranges of values rather than specific bit widths. For example, a short has to be able to represent, at a minimum, -32767 thru 32767 inclusive.

So,yes, if your code depends on values wrapping around at 32768, it's unlikely to behave well if the short is some big honking 128-bit behemoth.

If you want specific-width data types, look into stdint.h for things like int64_t and so on. There are a wide variety to choose from, specific widths, "at-least" widths, and so on. They also mandate two's complement for these, unlike the "regular" integral types:

  • integer types having certain exact widths;
  • integer types having at least certain specified widths;
  • fastest integer types having at least certain specified widths;
  • integer types wide enough to hold pointers to objects;
  • integer types having greatest width.

For example, from C11 7.20.1.1 Exact-width integer types:

The typedef name intN_t designates a signed integer type with width N, no padding bits, and a two’s complement representation. Thus, int8_t denotes such a signed integer type with a width of exactly 8 bits.

Provided you have followed the rules (things like not casting pointers to integers), your code should compile and run on any implementation, and any architecture.

If it doesn't, you'll just have to start debugging, then post the detailed information and code that seems to be causing problem on a forum site dedicated to such things. Now where have I seen one of those recently? :-)

Upvotes: 1

Related Questions