简体   繁体   English

Memcpy:添加int偏移量?

[英]Memcpy : Adding an int offset?

I was looking over some C++ code and I ran into this memcpy function. 我正在查看一些C ++代码,我遇到了这个memcpy函数。 I understand what memcpy does but they add an int to the source. 我理解memcpy的作用,但是他们在源代码中添加了一个int。 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的源代码,但我似乎无法理解添加实际上对memcpy函数做了什么。

memcpy(Destination, SourceData + intSize, SourceDataSize);

In other words, I want to know what SourceData + intSize is doing. 换句话说,我想知道SourceData + intSize正在做什么。 (I am trying to convert this to java.) (我试图将其转换为java。)

EDIT: 编辑:

So here is my attempt at doing a memcpy function in java using a for loop... 所以这是我尝试使用for循环在java中执行memcpy函数...

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

它是一样的:

memcpy(&Destination[0], &SourceData[intSize], SourceDataSize);

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. 地址更改的数量取决于SourceData的类型。

(See http://www.learncpp.com/cpp-tutorial/68-pointers-arrays-and-pointer-arithmetic/ ) (见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). 它可能正在尝试从偏移量intSize开始复制数组SourceData的一部分,并且长度为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: 因此,例如,如果数组的大小为4个字节的整数,那么等效的Java代码将如下所示:

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

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). SourceData指向某种数据类型,向其添加n会增加n * sizeof(* SourceData)指向的地址。

For example, if SourceData is defined as: 例如,如果SourceData定义为:

uint32_t *SourceData;

and

sizeof(uint32_t) == 4

then adding 2 to SourceData would increase the address it holds by 8. 然后将2添加到SourceData会将其保存的地址增加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. 另外,如果将SourceData定义为数组,则向其添加n有时与访问数组的第n个元素相同。 It's easy enough to see for n==0; 很容易看到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. 当n == 1时,很容易看到你将在数组开始后访问sizeof(* SourceData)字节的内存地址。

SourceData + intSize is skipping intSize * sizeof(source data type) bytes at the beginning of SourceData. SourceData + intSize在SourceData的开头跳过intSize * sizeof(源数据类型)字节。 Maybe SourceDataSize is stored there or something like that. 也许SourceDataSize存储在那里或类似的东西。

您可能会得到的最接近Java的memcpySystem.arraycopy ,因为Java实际上并没有指向相同的指针。

Regarding doing this in Java: 关于在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; 将始终开始将数据从0x100元素复制到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. (例如,当i=0Destination[0] = SourceData[0 + 0x100];依此类推。)如果您从未想要复制SourceData[0]..SourceData[0xFF] ,那么这就是您想要的SourceData[0]..SourceData[0xFF] ,但是请注意,硬编码可以防止它成为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). 在原始代码中指定intSize值的原因很可能是因为第一个intSize元素不是“实际”数据的一部分,并且这些字节以某种方式用于簿记(如缓冲区总大小的记录) 。 memcpy itself doesn't 'see' the offset; memcpy本身并没有“看到”偏移量; it only knows the pointer it's starting with. 它只知道它开始的指针。 SourceData + intSize creates a pointer that points intSize bytes past SourceData . SourceData + intSize创建一个指针,指向通过SourceData intSize字节。

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. memcpy是一个非常优化的函数,它映射到大多数体系结构上经过精心调整的程序集,并且用简单的每字节循环迭代替换它将极大地影响代码的性能特征。 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 . 如果您正在尝试理解memcpy和指针的工作方式 ,那么您正在做的事情是合适的 ,请注意,如果您尝试将现有代码移植到Java以供实际使用,您可能希望使用与java.util.Arrays.copyOf等道德等效的Java函数java.util.Arrays.copyOf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM