the malloc need to return an address to which every cell is empty
but when we print malloc1[i] (which is the content of cell "i")
we get the value -431602080.00000 instead of empty or some thing like that
why is that?
Printable View
the malloc need to return an address to which every cell is empty
but when we print malloc1[i] (which is the content of cell "i")
we get the value -431602080.00000 instead of empty or some thing like that
why is that?
Malloc doesn't return "empty" space in the sense that the content is "zero", but it is "empty" in the sense that the piece of memory that the address refers to is guaranteed to not be used by anyone else [as long as they follow the rules, at least].
If you want it to be zero then use calloc()[1]
[1] However, some will immediately point out that a bit-pattern of all zeros doesn't necessarily make a floating point zero, so if you are allocating memory for use as floating point values, you should, for complete portability, use a loop that sets each value as 0.0 (double) or 0.0f (float). That way you are sure the value is a zero-floating point value, rather than "all bits are zero".
--
Mats
> each of the 3 float elements will be initialized to zero
Read matsp's comment in the previous post.
And why wouldn't casting the return value of calloc() to the type float take care of that issue.
Because the issue isn't the type of the return value of calloc; the issue is that no one promises that "0x00000000" represents zero in your particular floating-point representation. (I don't know of any commonly used floating-point representation where it isn't, although in floating slash it might represent +Inf.)
As far as I know there is only the IEEE standard for representing floating point numbers and if the FPCP functionality exists alongwith the FPR's I don't see why the micro won't be able to intrepret that number as a float zero.
http://www.quadibloc.com/comp/cp0201.htm
Not all of these had all bits zero == 0.0
The C99 standard only refers to IEEE by example, to specify the minimum requirements for floating point support. It doesn't delve into the details of how a float is represented by the machine (in the same way that it makes no distinction about say endianess or 1 or 2's complement storage of integers).