Memcpy function equivalent from C ++ to Java

I have in C ++

memcpy (&wkpm, (PMSK *)pr_masks + (long)(x - 1), sizeof(PMSK)); 

where PMSKis the structure. This will be a class in Java.

Now, assuming that here I am copying the entire piece of memory into pr_masks, creating an additional instance of the class PMSK. How to do it in Java.

Example. In java code on line 20, I want to grab an instance of the class, and then use the same instance again on line 100. There can be many changes between them.

Hope I understood my question.

thank

+5
source share
4 answers

In Java, you need to either make a shallow clone()object, or copy each field separately. There is no low level, make one object a copy of another object.

+6

Java - memcpy(). Unsafe copyMemory(), memcpy(). , memcpy(), , .. , memcpy() memmove(). . , , ( JVM ).

Unsafe.copyMemory() ( 2 ). . , Unsafe JVM.

" Java" (http://highlyscalable.wordpress.com/2012/02/02/direct-memory-access-in-java/) "Java Magic. 4: sun.misc.Unsafe" ( memcpy ++ Java) . , ( ) Java.

+1

java.lang.System.arraycopy, . PMSK - , , . , Cloneable , .

, PMSK, , , , , . (java.awt.Color .) , , , java.lang.System.arraycopy .

0

In Java you have no pointers. If you have an instance of the object objyou want to copy, you must do it yourself. Although clone()suggested in other answers as an opportunity, I would avoid this. One thing you can do is use copy constructors (which you also have in C ++). You can read this about why it is clone()not recommended.

0
source

All Articles