Is it possible to get the memory address of a variable in C#.
What I am trying to do is very simple. I want to declare variables of type Double, Float, Decimal and assign the value 1.1 to each of these variables. Then I would like to go and see how these values are represented in memory. I need to get the memory address of the variable in order to see how its stored in memory. Once I have the memory address I plan to put a break point in the code and use the Debug -> Windows -> Memory option in visual studio to see how the numbers are stored in memory.
Cheers,
Yes, it is possible to obtain a raw pointer to storage in C#. Rather than try to explain it all here, I recommend that you read all of chapter 18 of the C# specification, which discusses this topic in detail.
However, if what you want to do is learn how various different floating point types store values, there are easier ways than looking at them in a debugger. These are all well-documented formats; you can just look them up in wikipedia or msdn and read about how they are laid out in memory.
The binary representation of a Decimal value consists of a 1-bit sign, a 96-bit integer number, and a scaling factor used to divide the 96-bit integer and specify what portion of it is a decimal fraction. The scaling factor is implicitly the number 10, raised to an exponent ranging from 0 to 28.
See http://msdn.microsoft.com/en-us/library/system.decimal.aspx for details.
The binary representation of a double is one sign bit, 11 exponent bits representing an exponent from -1022 to +1023, and 52 bits of mantissa, which are interpreted "1." followed by the 52 bits.
See http://en.wikipedia.org/wiki/Double_precision or my series of articles on floating point issues: http://blogs.msdn.com/ericlippert/archive/tags/Floating+Point+Arithmetic/default.aspx
A float is the same as a double, just half the size: one sign bit, 8 exponent bits, 23 mantissa bits. See http://en.wikipedia.org/wiki/Single_precision_floating-point_format for details.
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