从 source 复制 num 个字符到 destination,但是在重叠内存块这方面,memmove() 是比 memcpy() 更安全的方法。如果目标区域和源区域有重叠的话,memmove() 能够保证源串在被覆盖之前将重叠区域的字节拷贝到目标区域中,复制后源区域的内容会被更改。如果目标区域与源区域没有重叠,则和 memcpy() 函数功能相同。
memmove
Move block of memory
Copies the values of num bytes from the location pointed by source to the memory block pointed by destination. Copying takes place as if an intermediate buffer were used, allowing the destination and source to overlap.
The underlying type of the objects pointed by both the source and destination pointers are irrelevant for this function; The result is a binary copy of the data.
The function does not check for any terminating null character in source - it always copies exactly num bytes.
To avoid overflows, the size of the arrays pointed by both the destination and source parameters, shall be at least num bytes.
从 source 所指向的对象复制 num 个字节到 destination 所指向的对象。两个对象都被转译成 unsigned char 的数组。对象可以重叠:如同复制字符到临时数组,再从该数组到 destination 一般发生复制。
从 source 复制 num 个字符到 destination,但是在重叠内存块这方面,memmove() 是比 memcpy() 更安全的方法。如果目标区域和源区域有重叠的话,memmove() 能够保证源串在被覆盖之前将重叠区域的字节拷贝到目标区域中,复制后源区域的内容会被更改。如果目标区域与源区域没有重叠,则和 memcpy() 函数功能相同。
若出现 destination 数组末尾后的访问则行为未定义。
若 destination 或 source 为空指针则行为未定义。
void * memmove ( void * destination, const void * source, size_t num ) ;
Parameters
destination
Pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*.
指向用于存储复制内容的目标数组,类型强制转换为 void* 指针。
指向复制目的对象的指针
source
Pointer to the source of data to be copied, type-casted to a pointer of type const void*.
指向要复制的数据源,类型强制转换为 void* 指针。
指向复制来源对象的指针
num
Number of bytes to copy.size_t is an unsigned integral type.
要被复制的字节数。
要复制的字节数
Return Value
destination is returned.
该函数返回一个指向目标存储区 destination 的指针。
返回 destination 的副本,本质为更底层操作的临时内存地址,在实际操作中不建议直接使用此地址,操作完成以后,真正有意义的地址是destination本身。
Example
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int main () {
char str[32 ] = "I am your GOD" ;
char str2[32 ] = "Hello World" ;
char str3[] = "void * memmove ( void * destination, const void * source, size_t num );" ;
memmove(str2, str, strlen (str));
printf ("%s\n" , str2);
memmove(str2, "hi\0hi" , sizeof (str2));
printf ("%s\n" , str2);
printf ("%c%c%c%c\n" , str2[3 ], str2[4 ], str2[5 ], str2[6 ]);
memmove(str, str3, sizeof (str) - 1 );
for (int i = 0 ; i < sizeof (str); ++i) {
printf ("%c" , str[i]);
}
char str4[] = "1234567890" ;
puts (str4);
memmove(str4 + 4 , str4 + 3 , 3 );
puts (str4);
int *p = malloc (3 * sizeof (int ));
int arr[3 ] = {1 , 2 , 3 };
memmove(p, arr, 3 * sizeof (int ));
printf ("%d%d%d\n" , p[0 ], p[1 ], p[2 ]);
printf ("%d%d%d\n" , *p, *(p + 1 ), *(p + 2 ));
return 0 ;
}
文章参考
转载注明出处
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何统计不同电话号码的个数?—位图法
· C#高性能开发之类型系统:从 C# 7.0 到 C# 14 的类型系统演进全景
· 从零实现富文本编辑器#3-基于Delta的线性数据结构模型
· 记一次 .NET某旅行社酒店管理系统 卡死分析
· 长文讲解 MCP 和案例实战
· C#高性能开发之类型系统:从 C# 7.0 到 C# 14 的类型系统演进全景
· 管理100个小程序-很难吗
· 基于Blazor实现的运输信息管理系统
· 如何统计不同电话号码的个数?—位图法
· 微信支付功能的设计实现与关键实践(UniApp+Java)全代码