当前位置:网站首页>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 :

边栏推荐
- Analysis of the semaphore source code of AQS
- Leetcode skimming diary sword finger offer II 050. sum of downward path nodes
- MySQL index optimization
- 网络——传输层(详细版)
- Development of Quantitative Trading Robot System
- OJ 1045 reverse and add
- 等保3.0-服务器三权分立配置
- Project compilation nosuch*** error problem
- AQS之countDownLatch源码分析
- 下雨场景效果(一)
猜你喜欢

SSAO By Computer Shader(三)
![[dynamic planning -- the best period for buying and selling stocks Series 2]](/img/6c/887a026d3c1bcbd278bb7f3e0afd05.png)
[dynamic planning -- the best period for buying and selling stocks Series 2]

rancher部署实战
![Implementation of simple address book in [c language]](/img/75/8f2f4dd1c166808047cda6bea5a746.png)
Implementation of simple address book in [c language]

【二叉树基础知识】

设计测试用例的方法

NFT data storage blind box + mode system development

JS逆向100题——第1题

SSAO by computer shader (III)

Leetcode brush questions diary sword finger offer II 047. Binary tree pruning
随机推荐
Leetcode brush questions diary sword finger offer II 047. Binary tree pruning
Using C language to realize three piece chess games
Water bottle effect production
Explain in detail
Leetcode 刷题日记 剑指 Offer II 055. 二叉搜索树迭代器
[queue, simple application of stack ---- packaging machine]
Project compilation nosuch*** error problem
Mongo SSL configuration practice
[basic knowledge of binary tree]
OJ 1507 删数问题
SSAO By Computer Shader(一)
准备开始写博客了
数组转链表
InitializingBean接口及示例
SSAO by computer shader (I)
进程和线程的区别
[C language] string library function introduction and simulation
图形管线基础(二)
[C language] custom structure type
战疫杯--我的账本