Is the memory allocated for different data types will depend on the architecture?
Hi all is the memory allocated for different types of variable say float, int and char is different for different architecture? Tha开发者_开发技巧nks in advance.
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...
Yes, definitely. int
, in particular, is particularly prone to that: old 8-bit and 16-bit architectures invariably had 16-bit int
s, while today's 32-bit and 64-bit ones invariably use 32-bit int
s. That's how int
is defined to be -- the "natural" size of integers for the architecture you're compiling for!
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.
精彩评论