memcpy如何与void指针一起使用?

时间:2021-10-14 22:30:16

I am trying to memcpy from one ptr to another. I know the size that I want to copy. Both the destination and source pointers are void pointers. Is this valid? Does it actually copy the ELEMENT_SIZE (integer like 128) from source to the destination? I know this is not the most ideal thing to do. But I want to know if this works.

我试图从一个ptr到另一个ptr。我知道我要复制的大小。目标和源指针都是void指针。这有效吗?它是否实际上将ELEMENT_SIZE(整数类似于128)从源复制到目标?我知道这不是最理想的事情。但我想知道这是否有效。

memcpy(to_add, element_ptr, ELEMENT_SIZE);

3 个解决方案

#1


1  

Does it actually copy the ELEMENT_SIZE (integer like 128) from source to the destination?

它是否实际上将ELEMENT_SIZE(整数类似于128)从源复制到目标?

Yes, If you know the size information,then its working fine.

是的,如果您知道尺寸信息,那么它的工作正常。

See the reference link : http://www.cplusplus.com/reference/cstring/memcpy/

请参阅参考链接:http://www.cplusplus.com/reference/cstring/memcpy/

#2


1  

Parameter descriptions for memcpy from the documentation:

文档中memcpy的参数描述:

void * memcpy ( void * destination, const void * source, size_t num );

void * memcpy(void * destination,const void * source,size_t num);

destination: Pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*.

destination:指向要复制内容的目标数组的指针,类型转换为void *类型的指针。

source: Pointer to the source of data to be copied, type-casted to a pointer of type const void*.

source:指向要复制的数据源的指针,类型转换为const void *类型的指针。

num: Number of bytes to copy. size_t is an unsigned integral type.

num:要复制的字节数。 size_t是无符号整数类型。

memcpy simply takes num bytes starting from the address source and copies them to memory starting at address destination.

memcpy只需从地址源开始占用num个字节,然后从地址目的地开始将它们复制到内存中。

A pointer is a fixed length memory address, regardless of type. It does not matter whether the pointer is char * (points to character data), int * (points to integer data), or void * (points to data of unknown type), it still just points to memory.

无论类型如何,指针都是固定长度的存储器地址。无论指针是char *(指向字符数据),int *(指向整数数据)还是void *(指向未知类型的数据)都无关紧要,它仍指向内存。

Because memcpy copies an explicit number of bytes, the type of data being pointed to is irrelevant; it just need memory addresses to data.

因为memcpy复制了一个明确的字节数,所指向的数据类型是无关紧要的;它只需要数据的内存地址。

#3


0  

It does not matter what pointer it is really. It is a very plain process of having two memory addresses and engaging a copy.

它究竟是什么指针并不重要。这是一个非常简单的过程,有两个内存地址和一个副本。

#1


1  

Does it actually copy the ELEMENT_SIZE (integer like 128) from source to the destination?

它是否实际上将ELEMENT_SIZE(整数类似于128)从源复制到目标?

Yes, If you know the size information,then its working fine.

是的,如果您知道尺寸信息,那么它的工作正常。

See the reference link : http://www.cplusplus.com/reference/cstring/memcpy/

请参阅参考链接:http://www.cplusplus.com/reference/cstring/memcpy/

#2


1  

Parameter descriptions for memcpy from the documentation:

文档中memcpy的参数描述:

void * memcpy ( void * destination, const void * source, size_t num );

void * memcpy(void * destination,const void * source,size_t num);

destination: Pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*.

destination:指向要复制内容的目标数组的指针,类型转换为void *类型的指针。

source: Pointer to the source of data to be copied, type-casted to a pointer of type const void*.

source:指向要复制的数据源的指针,类型转换为const void *类型的指针。

num: Number of bytes to copy. size_t is an unsigned integral type.

num:要复制的字节数。 size_t是无符号整数类型。

memcpy simply takes num bytes starting from the address source and copies them to memory starting at address destination.

memcpy只需从地址源开始占用num个字节,然后从地址目的地开始将它们复制到内存中。

A pointer is a fixed length memory address, regardless of type. It does not matter whether the pointer is char * (points to character data), int * (points to integer data), or void * (points to data of unknown type), it still just points to memory.

无论类型如何,指针都是固定长度的存储器地址。无论指针是char *(指向字符数据),int *(指向整数数据)还是void *(指向未知类型的数据)都无关紧要,它仍指向内存。

Because memcpy copies an explicit number of bytes, the type of data being pointed to is irrelevant; it just need memory addresses to data.

因为memcpy复制了一个明确的字节数,所指向的数据类型是无关紧要的;它只需要数据的内存地址。

#3


0  

It does not matter what pointer it is really. It is a very plain process of having two memory addresses and engaging a copy.

它究竟是什么指针并不重要。这是一个非常简单的过程,有两个内存地址和一个副本。