Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Is it possible to get the address of Particular Object in c#?
plz help me..
Thanks in Advance
Posted
Updated 13-Aug-10 1:27am
v3

Memory address in .Net aren't static. Objects get moved around all the time by the memory manager. What are you trying to accomplish?
 
Share this answer
 
Comments
Roger Wright 14-Aug-10 2:39am    
Reason for my vote of 5
Good answer, and a good question, as it opens the door for further discussion and assistance.
Physical memory addresses are non-existant for applications in protected mode. This is a privileged operation that you as application do not have. You would also get nothing by simply reading physical addresses because you should also know what is on that physical address and due to paging that varies from time to time. The OS won't let you get in the way there and would be considered an security risk.
 
Share this answer
 
Well, you can do that. The keywords you should look for are "pointer pinning", "unsafe mode" and in general, "pointers in the managed run-time". But you have to be aware of the side effects of this - the Answer 1 already gives you a hint. Before you start working with pinned pointers and "unsafe" statements you should really answer the the question already asked above: "What are you trying to accomplish?" Pinning is rarely needed and I would strongly recommend to find another way...
 
Share this answer
 
Comments
Ganesh Kodiganti 19-Nov-23 20:58pm    
I was trying to implement memory efficient doubly linked list in C# and found this. By this, can I conclude that, Mem Efficient DLL can not be implemented in C# 'safely' ???
You can (without unsafe code) create a pinned GCHandle (and it must be pinned), then call AddrOfPinnedObject to get an IntPtr, which is the address of "that particular object". Optionally turn it into a pointer or long (preferably not an int)
But yes, what are you doing?
 
Share this answer
 
GCHandle objHandle = GCHandle.Alloc(YourObject, GCHandleType.WeakTrackResurrection);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900