Memory allocation clarification
I am trying to set a negative zero to an array in two ways. I need to know how the memory will be allocated for each type.
Type 1:
开发者_JAVA百科double dArray=new double[2];
dArray[0]= 0;
dArray[1]=-0;
Type 2:
double dArray=new double[2];
dArray[0]=0;
dArray[1]=-dArray[0];
Can any one clarify on this.
Thanks, Lokesh.
The code doesn't compile. I think you mean
double[] dArray=new double[2];
A double
is a value type and it makes no difference to its storage how you assign to a double
value. The two code samples (once fixed), result in exactly the same memory layout for dArray
.
It would be very much clearer to always write -0.0
.
Note that Visual Studio always represents negative zero as 0.0
which does tend to shake your confidence in the existence of -0.0
!
There is some relevant discussion of negative zero here: How can I test for negative zero?
I'd be interested in understanding your use case because ordinarily −0 = +0 = 0.
In any case they would both be treated as value types.
精彩评论