Jump to content
  • Advertisement
Sign in to follow this  
jackolantern1

[.net] How large are references?

This topic is 3159 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was wondering about this today, and I can't seem to find an answer. How large are the actual references held in reference type variables? I figure they are pretty small, and would likely be trivial for a desktop, but I am wondering if they may add up on mobile devices. Or am I completely misunderstanding the way that the references are handled in memory? I am just assuming that the variable holding the address to the object memory must be taking up some amount of space somewhere. Thanks!

Share this post


Link to post
Share on other sites
Advertisement
The easy answer is "it depends." In the CLR, object reference types w.r.t to the CIL instruction set are not exactingly defined -- the standard doesn't say how big they are. They're likely to be about the size of a value big enough to address the full range of memory on the system (although they may not be actual pointers; in my CLR they're just 1-based integers acting as handles into a relocatable pool that holds the actual object instance headers).

From ECMA-335
Quote:

Object references (type O) are completely opaque. There are no arithmetic instructions that allow object references as operands, and the only comparison operations permitted are equality and inequality between two object references. There are no conversion operations defined on object references. Object references are created by certain CIL object instructions (notably newobj and newarr). Object references can be passed as arguments, stored as local variables, returned as values, and stored in arrays and as fields of objects.

Share this post


Link to post
Share on other sites
Thanks! I appreciate the fast response. So I guess it would be safe to assume that they should not be too large then. When I was learning C++ pointers, I did the normal playing around with the & and * operators and saw the memory addresses of several different variables on two different computers, and neither was bigger than a short if I recall correctly.

Share this post


Link to post
Share on other sites
Quote:

and neither was bigger than a short if I recall correctly.

Pointers are often 32 bits on 32-bit systems. Do keep in mind however that this is implementation detail as well.

Share this post


Link to post
Share on other sites
Pointers tend to be the size of your CPU's word. If your process is running in WOW64 (32-bit windows application running on 64-bit windows; windows on windows 64 in short), then your pointers will be 32 bits.

The .net designers are zealous about "references != pointers". However reluctantly, even they admit that, in their essence ("current implementation, but that might change in the future"), references are pointers. Yes, more complicated, but still pointers. Therefore, to programatically know a reference's size, you can use IntPtr.Size.

Btw, in C++, pointers are exactly one word size. Again, there is the same complication with WOW64. However, in C++ you can easily determine that with the win32 api

Share this post


Link to post
Share on other sites
Quote:
Original post by dilyan_rusev
Btw, in C++, pointers are exactly one word size.

This is not always true, even without dealing with 32 bit programs running on 64-bit processors. It's quite frequently true, but not always true.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Quote:
Original post by dilyan_rusev
Btw, in C++, pointers are exactly one word size.

This is not always true, even without dealing with 32 bit programs running on 64-bit processors. It's quite frequently true, but not always true.


Agreeing with and building upon what SiCrane said, when dealing with a 32bit application compiled for a 32bit OS, pointers are most often 4bytes (2x a word size), though it's not always the case. References are often treated the same way in regards to size.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!