5

I have in C++

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

where PMSK is a struct. It will be a class in Java.

Now assuming that here I am copying the whole chunk of memory into pr_masks i.e creating an additional instance of the PMSK class. How to do this in Java.

Example: In a java code at line 20 I want capture the class instance and then again use that same instance in line 100. In between there may be many modifications.

Hope I am clear with my question.

Thanks

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
JavaBits
  • 2,005
  • 11
  • 36
  • 40
  • this can be done in c++ through pointers but in java how to approach? – JavaBits May 19 '11 at 14:23
  • [This](http://stackoverflow.com/questions/665860/deep-clone-utility-recomendation) might help you. – Sid May 19 '11 at 14:30
  • 2
    Copying object's memory is **NOT** creating another instance of object. – Tadeusz Kopec for Ukraine May 19 '11 at 14:30
  • By instance I meant to say a temporary holding structure. – JavaBits May 19 '11 at 14:41
  • As an alternative, you can use `JNI` or `JNA` approach to interface between C/C++ code and Java – eee May 19 '11 at 16:26
  • It might be better to focus on what you are trying to achieve (create a copy of some data) rather than the mechanism you are used to using in C++ (`memcpy()`). It seems you want to create a copy of an existing object. The correct way to do this in C++ and Java is with a copy constructor. – Raedwald Oct 07 '11 at 12:30
  • Side note: It is probably better to use std::copy if you are using C++ as opposed to C. http://stackoverflow.com/questions/4707012/c-memcpy-vs-stdcopy – elimirks Oct 30 '13 at 16:33

4 Answers4

6

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

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    -1 for having recommended `clone()`, which is often a good idea to [avoid](http://www.javapractices.com/topic/TopicAction.do?Id=71)! – MarcoS May 19 '11 at 14:27
  • @MarcoS, While it may be good to avoid clone() I don't see that any of the concerns apply here. There is no functionality implied by the OP's requirements which is problem and there are no more gotcha's than memcpy. BTW: I don't use clone() at all. I generate my data structures to have copy methods/constructors. – Peter Lawrey May 19 '11 at 14:41
  • @Peter Lawrey: maybe you're right, but still I would not recommend using a method that I don't trust. In this case, since the OP is familiar with C++, I would recommend a copy constructor, as I actually did in my [answer](http://stackoverflow.com/questions/6060163/memcpy-function-in-c-to-java-equivalent/6060334#6060334) :) – MarcoS May 19 '11 at 14:43
  • Would you care to elaborate your problem with clone()? Looking at teh wording of the original asker, I have the impression he is not very experienced in programming. I doubt: don't use clone() helps him much (I never had problems with clone(), so no idea to what you are reffering). – Angel O'Sphere May 19 '11 at 15:01
  • 1
    @Angel O'Sphere: I decided to avoid using clone after reading [Josh Bloch book "Effective Java"](http://www.amazon.com/Effective-Java-2nd-Joshua-Bloch/dp/0321356683). Some hints are also [here](http://www.artima.com/intv/bloch13.html) – MarcoS May 20 '11 at 09:59
  • @MacroS, I would agree that generally `clone()` is to be avoided. I think clone() is closest to memcpy, even having some of the same disadvantages. – Peter Lawrey May 20 '11 at 10:27
2

Java actually does have something just like memcpy(). The Unsafe class has a copyMemory() method that is essentially identical to memcpy(). Of course, like memcpy(), it provides no protection from memory overlays, data destruction, etc. It is not clear if it is really a memcpy() or a memmove(). It can be used to copy from actual addresses to actual addresses or from references to references. Note that if references are used, you must provide an offset (or the JVM will die ASAP).

Unsafe.copyMemory() works (up to 2 GB per second on my old tired PC). Use at your own risk. Note that the Unsafe class does not exist for all JVM implementations.

You should also take a look at "Tricks with Direct Memory Access in Java" (http://highlyscalable.wordpress.com/2012/02/02/direct-memory-access-in-java/) and "Java Magic. Part 4: sun.misc.Unsafe" (memcpy function in C++ to Java equivalent) for some additional ideas. These guys are deeply versed in how to do low level (and risky) operations in Java.

Community
  • 1
  • 1
Peter Schaeffer
  • 371
  • 4
  • 9
0

There's java.lang.System.arraycopy, but this is a shallow copy. If PMSK is a class, then the array will only contain pointers, and only the pointers will be copied. If you need a deep copy, you'll have to make the class Cloneable, and write your own loop, cloning each time.

I don't know what PMSK is, so it's hard to say, but generally, the best policy for value type objects is to make them immutable, deriving new objects instead of mutating them. (java.awt.Color might be an example of this.) If you do this, it doesn't matter whether you use deep copy or shallow copy, and java.lang.System.arraycopy should do the job.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • this class contains all byte[] array. So do you think arraycopy will be helpful? – JavaBits May 19 '11 at 14:41
  • @JavaBits It doesn't matter what the class contains. What's important is its interface; a shallow copy makes sense if the class is immutable. It might make sense otherwise as well, but you then have to take into account that the copy was shallow. As for an example, `System.arraycopy(pr_masks, 0, wkpm, 0, pr_masks.length)` should work, provided `wkpm` is sufficiently large and has the right type. – James Kanze May 19 '11 at 15:23
0

In Java you have no pointers. If you have an object instance obj that you want to copy then you must do it yourself. Although clone() is suggested in other answers as a possibility, I would avoid it. One thing that you could do is using copy constructors (that you also have in C++). You may want to read this on why clone() is not recommended.

MarcoS
  • 13,386
  • 7
  • 42
  • 63