in C#, is there a way to
- Get the memory address stored in a reference type variable?
- Get the memory address of a variable?
EDIT:
int i; int* pi = &i;
- How do you print out the hex value of pi?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
For #2, the
&operator will work in the same fashion as in C. If the variable is not on the stack, you may need to use afixedstatement to pin it down while you work so the garbage collector does not move it, though.For #1, reference types are trickier: you’ll need to use a
GCHandle, and the reference type has to be blittable, i.e. have a defined memory layout and be bitwise copyable.In order to access the address as a number, you can cast from pointer type to
IntPtr(an integer type defined to be the same size as a pointer), and from there touintorulong(depending on the pointer size of the underlying machine).