简体   繁体   中英

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? Thanks 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.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM