c memcpy 同内存_C / C ++ memcpy()–跨内存位置复制

c memcpy 同内存

The memcpy() function in C/C++ is used to copy data from one memory location to another. This is a common way of copying data using pointers.

C / C ++中的memcpy ()函数用于将数据从一个存储位置复制到另一个存储位置。 这是使用指针复制数据的常用方法。

Let’s understand how we can use this function in this article.

让我们了解如何在本文中使用此功能。



句法 (Syntax)

The memcpy() function takes in two memory address locations (src and dst) as arguments, along with the number of bytes (n) to be copied.

memcpy ()函数接受两个内存地址位置( srcdst )作为参数,以及要复制的字节数( n )。

Since C/C++ typically uses a void* pointer to denote memory location addresses, the invocation for the source and destination addresses are void* src and void* dst.

由于C / C ++通常使用void *指针表示内存位置地址,因此对源地址和目标地址的调用为void* srcvoid* dst

The function returns a pointer to the destination memory address dst, but in practice, we do not usually capture the return value. (Since we already have the pointer with us)

该函数返回一个指向目标内存地址dst的指针,但实际上,我们通常不捕获返回值。 (因为我们已经有了指针)

Format:

格式


#include 

void* memcpy(void* dst, const void* src, size_t n)

The header file is necessary to load this library function.

头文件是加载此库函数所必需的。

Arguments:

参数

  • dst -> Pointer to the destination address

    dst >指向目标地址的指针
  • src -> Pointer to the source address

    src >指向源地址的指针
  • n -> Number of bytes to be copied

    n >要复制的字节数

Return Value

返回值

This returns the pointer to the destination location (dst).

这会将指针返回到目标位置( dst )。

NOTE: To avoid any kind of overflow, the size of the arrays pointed to by both the destination and source arguments must be at least n bytes and cannot overlap.

注意 :为避免任何类型的溢出, 目标参数所指向的数组大小必须至少为n个字节,并且不能重叠。

Let’s understand the function using some examples.

让我们使用一些示例来了解该函数。



一些例子 (Some Examples)

Here is an example which copies a string from one location to another using memcpy().

这是一个使用memcpy()将字符串从一个位置复制到另一个位置的示例。


#include 
#include 

int main () {
    // This is constant, since memcpy() will not modify this
   const char src[50] = "JournalDev";
   char dest[50];

   strcpy(dest, "Sample");
   printf("Before memcpy(), dest: %s\n", dest);

   // It is strlen(src) + 1 since we also copy the null terminator '\0'
   memcpy(dest, src, strlen(src)+1);
   printf("After memcpy(), dest: %s\n", dest);
   
   return(0);
}

Output

输出量


Before memcpy(), dest: Sample
After memcpy(), dest: JournalDev

Here is another example which appends a string using memcpy() using appropriate offsets.

这是另一个示例,该示例使用memcpy()使用适当的偏移量附加字符串。


#include 
#include 

int main() {
    // This is constant, since memcpy() will not modify this
    const char src[100] = "This is a JournalDev article.";

    char dst[100] = "memcpy";
    
    printf("Before memcpy(), dst is: %s\n", dst);
    
    // Set offsets for both src and dst
    size_t offset_src = 0, offset_dst = 0;
    offset_src += 4;
    offset_dst += strlen(dst);

    memcpy(dst + offset_dst, src + offset_src, strlen(src));

    printf("After memcpy(), dst is: %s\n", dst);

    return 0;
}

The output will be the destination string appended with ” is a JournalDev article“.

输出将是目标字符串,后面附加“” 是JournalDev文章 ”。

Output

输出量


Before memcpy(), dst is: memcpy
After memcpy(), dst is: memcpy is a JournalDev article.


一个简单的memcpy()实现 (A Simple memcpy() Implementation)

Here is a simple implementation of memcpy() in C/C++ which tries to replicate some of the mechanisms of the function.

这是C / C ++中memcpy()一个简单实现,它试图复制该函数的某些机制。

We first typecast src and dst to char* pointers, since we cannot de-reference a void* pointer. void* pointers are only used to transfer data across functions, threads, but not access them.

我们首先将srcdst类型转换为char*指针,因为我们无法取消引用void*指针。 void*指针仅用于在函数,线程之间传输数据,而不能访问它们。

Now we can directly copy the data byte by byte and return the void* destination address.

现在,我们可以逐字节直接复制数据并返回void*目标地址。


void* memcpy_usr(void* dst, const void* src, size_t n) {
    // Copies n bytes from src to dst
    
    // Since we cannot dereference a void* ptr,
    // we first typecast it to a char* ptr
    // and then do the copying byte by byte,
    // since a char* ptr references a single byte
    char* char_dst = (char*) dst;
    char* char_src = (char*) src;

    for (int i=0; i

You can verify that by replacing memcpy() with our new function, we will get the same output.

您可以通过用新函数替换memcpy()来验证是否将获得相同的输出。



结论 (Conclusion)

In this article, we learned about the memcpy() library function in C/C++, which is useful to copy data from a source address to a destination address.

在本文中,我们了解了C / C ++中的memcpy()库函数,该函数对于将数据从源地址复制到目标地址很有用。



参考资料 (References)

  • Cplusplus.com page on memcpy()

    memcpy()上的Cplusplus.com页面


翻译自: https://www.journaldev.com/34452/c-memcpy-copy-across-memory-locations

c memcpy 同内存

你可能感兴趣的:(c memcpy 同内存_C / C ++ memcpy()–跨内存位置复制)