当前位置:网站首页>C language memcpy library functions and the role of memmove
C language memcpy library functions and the role of memmove
2022-07-28 06:48:00 【JuLiJuLi.】
1. Let's start with memcpy Return value and parameters of library function , Let's be more familiar with this library function

As you can see in the picture above memcpy The return value is (void*) Pointer to type
The first parameter dest It refers to that the target spatial location type of the copied content is strongly changed to void*.
The second parameter src The data source type to be copied is const Embellished void*, Why do we have to change to void* Because of this memcpy Library functions are not limited by type , Can accept many types .
The third parameter is the number of bytes to be copied .
The following is the simulation memcpy function
#include<stdio.h>
#include<assert.h>
void* My_memcpy(void* s1, const void* s2, size_t count) //s1 Namely p2 Is the target space to be copied
{ //s2 Namely p1 It is the data source that needs to be copied
assert(s1 && s2);
void* flag = s1;
while (count--)
{
*(char*)s1 = *(char*)s2;
s1 = (char*)s1+1;
s2 = (char*)s2+1;
}
return flag;
}
int main()
{
int p1[10] = { 1,2,3,4,5,6,7,8,9,10 };
int p2[5] = { 0 };
My_memcpy(p2, p1, 20);
int i = 0;
int sz = sizeof(p2) / sizeof(p2[0]); // Print p2 Contents of array
for (i = 0; i < sz; i++)
{
printf("%d ", p2[i]);
}
return 0;
}2. Let's see next memmove Return values and parameters of library functions .

We can see memmove The return value and parameters of this library function are the same as memcpy It's exactly the same , that memmove What is the function of ?
memcpy When operating in the same array ,dest The starting position of is the same as src When the starting positions of overlap, the effect of coverage will be poor , As a result, the program does not achieve the desired effect , The following figure explains in detail .


When we want to modify in the same array, we can use memmove This library function , It perfectly fills memcpy Disadvantages of library functions , So as to achieve the effect we want .
The following is a simulated implementation memmove function
#include<stdio.h>
#include<assert.h>
#include<string.h>
void* My_memmove(void* s1, const void* s2, size_t count)
{
assert(s1 && s2);
void* flag = s1;
if (s1 < s2)
{
while (count--) // From front to back
{
*(char*)s1 = *(char*)s2;
s1 = (char*)+1;
s2 = (char*)+1;
}
}
else
{
while (count--)
{
*((char*)s1 + count) = *((char*)s2 + count); // Copy from back to front
}
}
return flag;
}
int main()
{
int p1[10] = { 1,2,3,4,5,6,7,8,9,10 };
My_memmove(p1+2, p1, 20);
int i = 0;
int sz = sizeof(p1) / sizeof(p1[0]);
for (i = 0; i < sz; i++)
{
printf("%d ", p1[i]);
}
return 0;
}Detailed illustration :

边栏推荐
猜你喜欢

Redis implementation of distributed lock and analysis of the main process of redismission distributed lock

redis实现分布式锁思路及redission分布式锁主流程分析

Optimization ideas from ordinary query commodities to highly concurrent query commodities

Leetcode 刷题日记 剑指 Offer II 053. 二叉搜索树中的中序后继

Leetcode skimming diary sword finger offer II 050. sum of downward path nodes

MySQL index optimization

Mongodb replica set and partitioned cluster

About the collation of shader keyword

Battle plague Cup -- strange shape
![[dynamic planning -- the best period for buying and selling stocks series 3]](/img/9f/f6c07264f5ffaa0fdfcc724c713e83.png)
[dynamic planning -- the best period for buying and selling stocks series 3]
随机推荐
Leetcode 刷题日记 剑指 Offer II 055. 二叉搜索树迭代器
2021-11-10
RayMarching realizes volume light rendering
OJ 1131 beautiful number
OJ 1045 reverse and add
AQS之ReentrantLock源码解析
Question brushing record -- binary tree
redis缓存设计与性能优化
Pyppeteer is recognized to bypass detection
Yapi vulnerability hanging horse program chongfu.sh processing
Optimization ideas from ordinary query commodities to highly concurrent query commodities
JS variable is equal to 0, etc. '
Graphic pipeline foundation (I)
How to simulate the implementation of strcpy library functions
Array solution script
Pyppeter drop-down selenium drop-down
等保3.0-服务器三权分立配置
SSAO By Computer Shader(二)
OJ 1505 fuse
网络通信及TCP/IP协议