4

I was looking over some C++ code and I ran into this memcpy function. I understand what memcpy does but they add an int to the source. I tried looking up the source code for memcpy but I can't seem to understand what the adding is actually doing to the memcpy function.

memcpy(Destination, SourceData + intSize, SourceDataSize);

In other words, I want to know what SourceData + intSize is doing. (I am trying to convert this to java.)

EDIT:

So here is my attempt at doing a memcpy function in java using a for loop...

for(int i = 0 ; i < SourceDataSize ; i ++ ) {
      Destination[i] = SourceData[i + 0x100];
}
VeeArr
  • 6,039
  • 3
  • 24
  • 45
user1007682
  • 43
  • 1
  • 1
  • 4
  • It's nothing to do with memcpy, it's just very basic pointer arithmetic – David Heffernan Feb 24 '12 at 20:28
  • 8
    How to convert from language A to completely unrelated language B: 1) understand what the original code is supposed to do; 2) understand how the original code does what it is supposed to do; 3) figure out how that can be done in language B; 4) write the code. The question seems to be missing steps #1 and #2. You'll have a hard time getting good answers. – R. Martinho Fernandes Feb 24 '12 at 20:28
  • @R.Martinho, one way of understanding what code is *supposed* to do is to figure out what it *does*. Thus, I think this question goes toward accomplishing step 1. – Rob Kennedy Feb 24 '12 at 20:42
  • @R.MartinhoFernandes Really? because i'm pretty sure that i stated clearly that i DO NOT UNDERSTAND what it is doing. Hence the question? – user1007682 Feb 24 '12 at 21:34

6 Answers6

12

It is the same thing as:

memcpy(&Destination[0], &SourceData[intSize], SourceDataSize);
ronag
  • 49,529
  • 25
  • 126
  • 221
1

The add will change the address used for the source of the memory copy.

The amount the address changes will depend on the type of SourceData.

(See http://www.learncpp.com/cpp-tutorial/68-pointers-arrays-and-pointer-arithmetic/)

It might be trying to copy a section of an array SourceData starting at offset intSize and of length SourceDataSize/sizeof(*SourceData).

EDIT

So, for example, if the array was of integers of size 4 bytes, then the equivalent java code would look like:

for(int i = 0 ; i < SourceDataSize/4 ; i ++ ) {
  Destination[i] = SourceData[i + intSize];
}
Peter de Rivaz
  • 33,126
  • 4
  • 46
  • 75
1

This is basic pointer arithmetic. SourceData points to some data type, and adding n to it increases the address it's pointing to by n * sizeof(*SourceData).

For example, if SourceData is defined as:

uint32_t *SourceData;

and

sizeof(uint32_t) == 4

then adding 2 to SourceData would increase the address it holds by 8.

As an aside, if SourceData is defined as an array, then adding n to it is sometimes the same as accessing the nth element of the array. It's easy enough to see for n==0; when n==1, it's easy to see that you'll be accessing a memory address that's sizeof(*SourceData) bytes after the beginning of the array.

Ori Pessach
  • 6,777
  • 6
  • 36
  • 51
1

SourceData + intSize is skipping intSize * sizeof(source data type) bytes at the beginning of SourceData. Maybe SourceDataSize is stored there or something like that.

whatever
  • 11
  • 1
1

The closest equivalent to memcpy in Java that you're probably going to get is System.arraycopy, since Java doesn't really have pointers in the same sense.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
0

Regarding doing this in Java:

Your loop

for(int i = 0 ; i < SourceDataSize ; i ++ ) {
      Destination[i] = SourceData[i + 0x100];
}

will always start copying data from 0x100 elements into SourceData; this may not be desired behavior. (For instance, when i=0, Destination[0] = SourceData[0 + 0x100]; and so forth.) This would be what you wanted if you never wanted to copy SourceData[0]..SourceData[0xFF], but note that hard-coding this prevents it from being a drop-in replacement for memcpy.

The reason the intSize value is specified in the original code is likely because the first intSize elements are not part of the 'actual' data, and those bytes are used for bookkeeping somehow (like a record of what the total size of the buffer is). memcpy itself doesn't 'see' the offset; it only knows the pointer it's starting with. SourceData + intSize creates a pointer that points intSize bytes past SourceData.

But, more importantly, what you are doing is likely to be extremely slow. memcpy is a very heavily optimized function that maps to carefully tuned assembly on most architectures, and replacing it with a simple loop-per-byte iteration will dramatically impact the performance characteristics of the code. While what you are doing is appropriate if you are trying to understand how memcpy and pointers work, note that if you are attempting to port existing code to Java for actual use you will likely want to use a morally equivalent Java function like java.util.Arrays.copyOf.

shanef22
  • 323
  • 1
  • 5