简体   繁体   English

真的需要memcpy start index吗?

[英]memcpy start index really needed?

The question is when we are copying any Byte array using memcpy() , shall we explicitly declare the starting (0 th) index for the destination buffer or simple mentioning it would suffice. 问题是当我们使用memcpy()复制任何字节数组时,我们是否应该为目标缓冲区显式声明起始(0)索引,或者简单地提及它就足够了。 Let me show the examples what I'm talking about. 让我举例说明我在说什么。 Provided that we are trying to copy source buffer to starting of the destination buffer. 前提是我们正在尝试将源缓冲区复制到目标缓冲区的启动。

BYTE *pucInputData; // we have some data here
BYTE ucOutputData[20] = {0};

Code 1 代码1

memcpy((void*)&ucOutputData, (void*)pucInputData, 20);

Code 2 代码2

memcpy((void*)&ucOutputData[0], (void*)pucInputData, 20);

In your case, considering this a C code snippet, and ucOutputData is an array 在您的情况下,考虑到这是一个C代码片段,而ucOutputData是一个数组

  • memcpy(ucOutputData, pucInputData, 20);
  • memcpy(&ucOutputData[0], pucInputData, 20);

both are same and can be used Interchangeably. 两者都相同 ,可以互换使用。 The name of the array essentially gives you the address of the first element in the array. 数组的名称实际上为您提供了数组中第一个元素的地址。

Now, as per the very useful discussion in below comments, it is worthy to mention, that 现在,根据以下评论中非常有用的讨论,值得一提的是

memcpy(&ucOutputData, pucInputData, 20);

will also do the job here, but there is a fundamental difference between the usage of array name and address of array name . 也会在这里完成这项工作,但是数组名称的使用和数组名称的 地址之间存在根本区别 Considering the example in the question, for a definition like BYTE ucOutputData[20] , 考虑到问题中的例子,对于像BYTE ucOutputData[20]这样的定义,

  • ucOutputData points to the address of the first element of an array of 20 BYTE s. ucOutputData指向20个BYTE的数组的第一个元素的地址。
  • &ucOutputData is a pointer to an array of 20 BYTE s. &ucOutputData是一个指向20个BYTE数组的指针。

So, they are of different type and C respects the type of the variable. 因此,它们是不同类型的,C尊重变量的类型。 Hence, to avoid any possible misuse and misconception, the recommended and safe way to use this is either of the the first two expressions. 因此,为了避免任何可能的误用和误解,使用它的推荐和安全的方法是前两个表达式中的任何一个。

FWIW, the cast(s) here is(are) really not needed. FWIW,这里的演员真的不需要。 Any pointer type can be implicitly ansd safely be converted to void * in C. 可以隐式地将任何指针类型转换为C中的void *

Since an expression &array[0] is the same as array , and because any pointer can be implicitly converted to void* , you should do this instead: 由于表达式&array[0]是相同的array ,而且由于任何指针可以被隐式转换为void* ,你应该这样做,而不是:

memcpy(ucOutputData, pucInputData, 20);

Moreover, since you are writing over the entire ucOutputData , you do not need to zero out its content, so it's OK to drop the initializer: 此外,由于您正在编写整个ucOutputData ,因此不需要将其内容清零,因此可以删除初始化程序:

BYTE ucOutputData[20]; // no "= {0}" part

No, both of your examples are sub-optimal. 不,你的两个例子都不是最佳的。

Remember that all data pointers in C convert to/from void * (which is the type of the first argument to memcpy() ) without loss of information and that no cast is necessary to do so. 请记住,C中的所有数据指针都转换为/来自void * (这是memcpy()的第一个参数的类型),而不会丢失信息,并且不需要强制转换。

Also remember that the name of an array evaluates to the address of the first element in many contexts, such as here. 还要记住,数组的名称求值为许多上下文中第一个元素的地址,例如此处。

Also remember to use sizeof when you can, never introduce a literal constant when you don't have to. 还记得尽可能使用sizeof ,当你不需要时,永远不要引入文字常量。

So, the copy should just be: 所以,副本应该只是:

memcpy(ucOutputData, pucInputData, sizeof ucOutputData);

Note that we use sizeof without parentheses, it's not a function. 请注意,我们使用sizeof而没有括号,它不是一个函数。 Also we use it on the destination buffer, which seems the safer choice. 我们也在目标缓冲区上使用它,这似乎是更安全的选择。

A native array can decay to a pointer without conversion, so in the snippet below, the three assignments to p all result in the same value; 原生数组可以在没有转换的情况下衰减到指针,因此在下面的代码段中,对p的三个赋值都会产生相同的值; p will point to the beginning of the array. p将指向数组的开头。 No explicit cast is needed because casting to void* is implicit. 不需要显式转换,因为转换为void*是隐式的。

typedef char BYTE;
BYTE ucOutputData[20] = {0};

void *p = &ucOutputData;
p = ucOutputData;
p = &ucOutputData[0];

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

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