1
Answer

copy struct

john ranella

john ranella

10y
1.2k
1
Hi,
I'm trying to convert this code written in c to c#, in particular I'm looking for copy a struct from memory area to another memory area 

Example in C
memcpy(&LastRestore, &struct, sizeof(struct));


How can i implement this in C#?

this is an example of struct:

public struct a
{

public a(uint i): this()
{


z = new bool[14];

}
public double a;
public float c;
public ushort f;
public short h;
public bool l;
public bool[] z;
}
Answers (1)
0
Vulpes

Vulpes

NA 96k 2.6m 10y
Normally, in C#, you have no control over memory allocations - and don't need any. The CLR does a good job of managing memory for you.

As structs are value types simply assigning them to a different variable copies them to a new memory location:

a va = new a();
a vb = va; // va's contents have been copied to vb's memory location

However, it is possible - by various means - to copy managed structs to the unmanaged heap and back again, as the CLR has no control of unmanaged memory.

So what exactly are you trying to do here?