Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm seeing code allocating memory for float using sizeof(int).
I'm wondering whether sizeof(float) always equal to sizeof(int) on all architectures?

float *pointer2Float = (float *) USER_DEFINED_MALLOC (...,..., sizeof(int))

Note: this USER_DEFINED_MALLOC isa wrapper for conventional malloc, I think.

Thanks

Regards

like image 968
elgnoh Avatar asked Aug 06 '12 17:08

elgnoh


People also ask

Is size of float and int same?

They are totally different - typically int is just a straightforward 2's complement signed integer, while float is a single precision floating point representation with 23 bits of mantissa, 8 bits exponent and 1 bit sign (see http://en.wikipedia.org/wiki/IEEE_754-2008).

What is the sizeof float in your system?

float variables normally require 4 bytes of memory space. double - This is used for storing double precision floating point values or decimal values. Double variables normally require 8 bytes of memory space. char - This is used for storing characters.

Does sizeof return an int?

sizeof(int) returns the number of bytes used to store an integer. int* means a pointer to a variable whose datatype is integer. sizeof(int*) returns the number of bytes used to store a pointer.

Why size of float is 4 bytes?

The size of a float or other data types for that matter is dependent upon the system. It has to do with the hardware architecture and the compiler. This float, 10498.429 , would also be 4 bytes in memory. If a given computer system had a float size of 4 bytes then all floats are 4 bytes.


2 Answers

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.

like image 80
Daniel Fischer Avatar answered Oct 27 '22 00:10

Daniel Fischer


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!

like image 30
Nawaz Avatar answered Oct 27 '22 00:10

Nawaz