开发者

Memory address of a variable

开发者_如何学JAVAIs 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 just want to see how things get represented in memory then use the BitConverter class which will give you back an array of bytes. You can get the address of variables in C# but there probably isn't much point.

Of course BitConverter doesn't handle decimals so what you can do is use the debug memory window and in the scope of the variable just use the syntax &varname and it will work out the address for you.


Agree with tyranid , thet internally they will be reperesented in bits and getting their memory address witll not serve any purpose. On the other hand you can use ILdasm to inspect the actual type generated by compiler for the values you specify in your C# code.Consider following decleration in C# :-

double d1 = 21.1;
decimal d2 = 22.1M;
float d3 = 21.1F;

If you inspect the IL code through ILDasm , then this is what is generated by C# compiler for the above decleration :-

//000012:             double d1 = 21.1;
  IL_0001:  ldc.r8     21.100000000000001
..............
//000013:             decimal d2 = 22.1M;
  IL_000b:  ldc.i4     0xdd
...............
//000014:             float d3 = 21.1F;
  IL_001a:  ldc.r4     21.1

In here you can see that decimal is internally represented as Hex number.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜