Using memcpy to copy managed structures

Posted by Haris Hasan on Stack Overflow See other posts from Stack Overflow or by Haris Hasan
Published on 2010-04-01T11:48:29Z Indexed on 2010/04/01 11:53 UTC
Read the original article Hit count: 498

Filed under:
|

Hi,

I am working in mixed mode (managed C++ and C++ in one assembly). I am in a situation something like this.

ManagedStructure ^ managedStructure = gcnew ManagedStructure(); //here i set different properties of managedStructure

then I call "Method" given below and pass it "& managedStructure"

Method(void *ptrToStruct) { ManagedStructure ^ managedStructure2 = gcnew ManagedStructure(); memcpy(&managedStructure2 , ptrToStruct, sizeof(managedStructure2 )); }

I have following question about this scenario.

1) Is it safe to use memcpy like this? and if not what is its alternate to achieve same functionality? ( I can't change "Method" definition)

2) I am not freeing any memory as both the structures are managed. Is it fine?

© Stack Overflow or respective owner

Related posts about memcpy

Related posts about managed-c++