博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c memcpy 同内存_C / C ++ memcpy()–跨内存位置复制
阅读量:2534 次
发布时间:2019-05-11

本文共 4524 字,大约阅读时间需要 15 分钟。

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 <string.h> is necessary to load this library function.

头文件<string.h>是加载此库函数所必需的。

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: SampleAfter 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: memcpyAfter 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)



翻译自:

c memcpy 同内存

转载地址:http://qvqzd.baihongyu.com/

你可能感兴趣的文章
欧建新之死
查看>>
自定义滚动条
查看>>
APP开发手记01(app与web的困惑)
查看>>
笛卡尔遗传规划Cartesian Genetic Programming (CGP)简单理解(1)
查看>>
mysql 日期时间运算函数(转)
查看>>
初识前端作业1
查看>>
ffmpeg格式转换命令
查看>>
万方数据知识平台 TFHpple +Xpath解析
查看>>
Hive实现oracle的Minus函数
查看>>
秒杀多线程第四篇 一个经典的多线程同步问题
查看>>
RocketMQ配置
查看>>
vs code调试console程序报错--preLaunchTask“build”
查看>>
蚂蚁金服井贤栋:用技术联手金融机构,形成服务小微的生态合力
查看>>
端口号大全
查看>>
机器学习基石笔记2——在何时可以使用机器学习(2)
查看>>
POJ 3740 Easy Finding (DLX模板)
查看>>
MySQL 处理重复数据
查看>>
关于typedef的用法总结(转)
查看>>
【strtok()】——分割字符串
查看>>
Linux下安装rabbitmq
查看>>