Does sizeof(float) always equal to sizeof(int) on all architectures?

喜欢而已 提交于 2019-12-05 08:56:58

No, there are implementations (mainly embedded systems) with 16-bit int and 32-bit float.

And of course, the sizes are allowed to be quite different per the standard.

The sizes of ALL types (except char, signed char and unsigned char1) are implementation-defined. So it is not guaranteed that sizeof(float) will be equal to sizeof(int) on all platforms.

1. The size of char and all its variants is defined to be 1-byte by the Standard. However, the number of bits in 1-byte is implementation-defined!

No, sizeof (int) and sizeof (float) values are implementation-defined and are not guaranteed to be the same.

Here is an example of system where the two values are different:

On Cray MPP systems:

sizeof (int)  is 8

sizeof (float) is 4

See here:

"Cray C/C++ Reference Manual", Table 3. Cray Research systems data type mapping in "9.1.2.2 Types"

http://docs.cray.com/books/004-2179-003/004-2179-003-manual.pdf

Also most 8-bit embedded systems have an int of 16-bit wide with sizeof (int) is 2 while floating point follow IEEE-754 and float are 32-bit with sizeof (float) of 4.

No. In C and C++, data types are platform specific. In general, this will mean:

But for other systems, the general specification is that int has the natural size suggested by the system architecture (one "word") and the four integer types char, short, int and long must each one be at least as large as the one preceding it, with char being always one byte in size. The same applies to the floating point types float, double and long double, where each one must provide at least as much precision as the preceding one.

(Taken from Data Types)

On many platforms, float and int are both often 32bit, but this isn't always the case, nor is it part of the actual specification.

There is absolutely no guarantee that sizeof (float) be equal to sizeof (int), and I'd consider the above to be a coding error.

It should use sizeof *ptrToFloat in favor of either sizeof (int) or sizeof (float).

If you are writing a multi platform that size of data type is very important, you can create an header file such as :

#define INT sizeof(int)
#define FLOAT sizeof(float)
.
.
.

It's a trick to cheat arch.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!