-2

Is it possible to change the memory address of an object in C# ? Example of what I want:

unsafe public Program {

public struct Foo
    {
        int a;
        int b;

    }
 static void Main(string[] args){

        Foo foo= new Foo();
        fixed(Foo *foo_ptr=&foo)   //I know I can get the object pointer here.  
        Foo foo2= new Foo();
        fixed(&foo2=foo_ptr)      //How I can do this?
}
}

I got this error when I tried the last fixed: You can only take the address of an unfixed expression inside of a fixed statement initializer csharp(CS0212)

Can someone help me? Thanks.

Edit: There is long story why I want change the memory address and not just writing that foo2=foo; . That is why I am wondering if it is possible or not? This post is not what I am asking

  • It's quite unclear what you're trying to do. If you want to assign `foo` to `foo2`, just write `foo2 = foo`, this will copy the whole struct. Structs are value types (by definition), so it's not possible to "change their address", but this isn't necessary either, due to their value semantics. Alternatively, you may be looking for a `ref` variable (`ref Foo foo2 = ref foo;`). – Jeroen Mostert Mar 29 '22 at 13:13
  • Does this answer your question? [c# change variable memory address](https://stackoverflow.com/questions/30416499/c-sharp-change-variable-memory-address) – Dominik Mar 29 '22 at 13:13
  • Thanks for your comment. I wrote an example but there is important reason why I want to change the memory address. The reason is that I am using pointer that is coming from C++ (shared library). The C++ pointer is pointing to an object in another C++ process, and I want to make the both objects pointing to the same shared memory address. That is why I want change the C# object memory address. – Magnus Zaza Mar 29 '22 at 13:18
  • @MagnusZaza I still don't quite get what you want to do. If you have a pointer to an object in a different process, what does creating a new object in your process then changing it's address to the other objects address accomplish? Why not just use the pointer that you've received? – HasaniH Mar 29 '22 at 13:29
  • 1
    Hi. It won't be easy to do so. Because in .Net, GC must track the address of all of the variables to de-allocate the pointer, after releasing the last anchor of the variable. Also, it's illegal because each application has it's own Application Domain and by default, no application is allowed to access to of its boundary. – Mohammad Mirmostafa Mar 29 '22 at 13:32
  • @HasaniH Thanks for your comment. I want to assign the pointer that I have received to the C# object and then the both objects from C++ and C# will be connected to the same shared memory file descriptor. – Magnus Zaza Mar 29 '22 at 13:36
  • @MohammadMirmostafa Thanks for your comment, This was helpful. Do you have any sources about this? – Magnus Zaza Mar 29 '22 at 13:39
  • Actually this is the basic of .NET. You can find it in MSDN. For instance [this link](https://learn.microsoft.com/en-us/dotnet/standard/garbage-collection/), might be useful. – Mohammad Mirmostafa Mar 29 '22 at 13:47
  • You do not get pointers, then assign them to objects; you get objects, then receive pointers from them. If you want to ensure data at location A matches location B, you'll have to copy data. You can pin a pointer and access data through a managed language that way, or declare a `Span` or `Memory` to wrap that data, but what you cannot do is store a managed object in unmanaged memory -- by the very definition of "managed object". Note that .NET provides access to shared memory using `MemoryMappedFile` and `UnmanagedMemoryAccessor`. – Jeroen Mostert Mar 29 '22 at 13:52

2 Answers2

0

Only to mark this question as answered to be useful, I pasted my comment here again.

It won't be easy to do so. Because in .Net, GC must track the address of all of the variables to de-allocate the pointer, after releasing the last anchor of the variable. Also, it's illegal because each application has it's own Application Domain and by default, no application is allowed to access to out of its boundary.

Please consider marking the question as answered. Thanks a lot.

Mohammad Mirmostafa
  • 1,720
  • 2
  • 16
  • 32
0

Suppose p is the pointer to the shared memory, you can use ref keyword to create a reference.

byte* p;
ref Foo foo = ref *(Foo*)p;
//foo.a, foo.b
shingo
  • 18,436
  • 5
  • 23
  • 42