3

I am supposed to do a project in C# to present how a list does work.

I'm thinking it's a good idea to show in a graphic interface like this:

-in "value" field: value of node
-in "next" field: physical address of next node

Is this possible?

Stephan Bauer
  • 9,120
  • 5
  • 36
  • 58
Dan
  • 139
  • 2
  • 3
  • 10
  • Your `List` implementation can contain *high level* memory management as well, if your intent is to demonstrate how memory is used. – Sinatr Dec 19 '14 at 09:07
  • 1
    unless the variable is `fixed` there is no guarantee it won't change location.http://msdn.microsoft.com/en-gb/library/f58wzh21%28v=vs.80%29.aspx – Jodrell Dec 19 '14 at 09:08
  • It's worth noting that managed variables can generally be moved around during a garbage collection, as the garbage collector can (and will) defragment memory – Rowland Shaw Dec 19 '14 at 09:10
  • 3
    Note that you can't get a physical address. All what you can get is virtual address – Sriram Sakthivel Dec 19 '14 at 09:11
  • http://msdn.microsoft.com/en-gb/library/zcbcf4ta%28v=vs.80%29.aspx – Jodrell Dec 19 '14 at 09:31
  • @Jodrell note that stack variables (locals) won't move around; instance variables (fields) can though, yes – Marc Gravell Dec 19 '14 at 10:22
  • Dan, did my answer help you? If not, please tell me, otherwise, it would be awesome if you can accept it as the answer to your question =) – Ray Oct 03 '15 at 18:17

1 Answers1

4

Yes, with unsafe code (at least the virtual address, not the raw physical memory address, but that's just what you want).

Have a look at MSDN: How to: Obtain the Address of a Variable (C# Programming Guide) for details. It guides you through compiling the following example (it requires unsafe code to be enabled in the project properties or the /unsafe switch) and what traps you could experience when getting the location of moveable variables, which require the fixed keyword to stay at the same location, basically said.

int number;
int* p = &number; //address-of operator &
Ray
  • 7,940
  • 7
  • 58
  • 90