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
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).
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.
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.
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.
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 char
1) 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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With