tags:

views:

55

answers:

3

Hi all is the memory allocated for different types of variable say float, int and char is different for different architecture? Thanks in advance.

+2  A: 

Yes, definitely. int, in particular, is particularly prone to that: old 8-bit and 16-bit architectures invariably had 16-bit ints, while today's 32-bit and 64-bit ones invariably use 32-bit ints. That's how int is defined to be -- the "natural" size of integers for the architecture you're compiling for!

Alex Martelli
@Alex `int` can be a non-native word size. gcc for OS X these days is like that - 64-bit programs, but `int` is 32-bits.
Carl Norum
@Carl, of course -- I did mention that 64-bit architectures' C compilers "invariably" (a bit of an overbid;-) use 32-bit ints -- they get away with it because, also "invariably" (;-), 64-bit architectures impose no speed penalty for dealing w/32-bit ints (not true for 32-bit architectures and 16-bit ints, in many cases).
Alex Martelli
@Alex, yup. I just wanted to clarify about the "natural" size not necessarily being `int`.
Carl Norum
+4  A: 

It's definitely the case that float, int, and char may be of different sizes on different devices, yes. It's implementation-defined by your C compiler. All you can count on for really portable code is that:

sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long)

And that sizeof(char) == 1. There are a bunch of types in C99 that are of specific bit sizes, those may be useful to you if you need to keep type size portable from architecture to architecture.

Edit: I looked up the information in the spec. Section 5.2.4.2.1, "Sizes of integer types", is what you're looking for:

...implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown...

UCHAR_MAX         255 // 2^8 - 1
USHRT_MAX       65535 // 2^16 - 1
UINT_MAX        65535 // 2^16 - 1
ULONG_MAX  4294967295 // 2^32 − 1

And so on...

Carl Norum
So the code written in the 64 bit architecture may not work in 16 bit or 32 bit architecture right?
PrithviRaj
@prithviraj, that's right. It might work, if you get lucky.
Carl Norum
A: 

As others have said, it's a complete YES. However, there's also the part about endianness which nobody has mentioned. Different architectures can store the bytes that make up a type in different orders as well.

sharth