当前位置:网站首页>Deep analysis of string -- memcpy & memmove
Deep analysis of string -- memcpy & memmove
2022-07-21 20:28:00 【Shilipo Xiaobai】
List of articles
Depth analysis :strcat & strncat
Depth analysis :strchr & strstr
Depth analysis :strcmp & strncmp
Depth analysis :strcpy & strncpy
Depth analysis :strlen & strtok
Depth analysis :memcpy & memmove
Depth analysis :memset & memcmp
List of articles
Preface
Library function string It is also very important code that we often use , I will deeply analyze the usage and implementation principle of commonly used functions
One 、memcpy
1. Call structure
void *memcpy(void *dest, const void *src, size_t n);
2. The use of,
memcpy: from src Copy count Byte to dest
The principle, :
- Memory copies one byte by one
- Copy the value of each byte stored in memory
- It is no longer limited to the copy of strings
3. Source analysis
void *Memcpy(void *dest, const void *src, size_t count) {
// Memory copy : from src Copy count Byte to dest
assert(dest && src);
char *start = dest; // Record the address of the starting position
while (count--) {
// Number of bytes copied
*(char *) dest = *(char *) src; // Copy byte by byte
dest = (char *) dest + 1;
src = (char *) src + 1;
}
return start; // Return the address of the starting position
}
Two 、memmove
1. Call structure
void *memmove (void *destination, const void *source, size_t num);
2. The use of,
memmove: from src Copy count Byte to dest
The principle, :
- And memcpy The difference is that memmove The source and target memory blocks processed by the function can overlap
- If the source space and the target space overlap , You have to use memmove Function processing
3. Source analysis
void *Memmove(void *dest, void *src, int count) {
// Memory copy : from src Copy count Byte to dest
assert(dest && src); // Prevent passing null pointers
char *start = dest;
if (dest < src) {
// front -> after
while (count--) {
*(char *) dest = *(char *) src;
dest = (char *) dest + 1;
src = (char *) src + 1;
}
} else {
// after -> front
while (count--) {
*((char *) dest + count) = *((char *) src + count);
}
}
return start;
}
边栏推荐
猜你喜欢
随机推荐
Hetai 32 onenet WiFi module - Hetai MCU data cloud through mqtt protocol (I)
Those pits in distributed transactions
(笔记)吴恩达深度学习L4W2
深度剖析 string —— strlen & strtok
小游戏类项目 —— 扫雷
不拉扯了 不拉扯了 碎碎念 下
C. Binary String(求前缀和)
PCL学习第九章《采样一致性》
Datalosserror: corrected record at XXXXXXX, Bert pre training error
从一个点云中提取一个子集
1024 scientific counting method
Codeforces 1642B Power Walking
25.【判断是否是素数的方法】
Examples of enumeration
第五局 阿卡丽教学局 上
Pre training and fine tuning of Google Bert model for beginners
Operating instructions for opt101 monolithic photodiode and single power supply mutual resistance amplifier
如何逐步匹配多幅点云
10.【file的打开格式及其判断是否打开】
深度剖析 string —— strcpy & strncpy