当前位置:网站首页>Usage of memory operation functions memcpy() and memmove()
Usage of memory operation functions memcpy() and memmove()
2022-07-28 11:03:00 【tutu-hu】
One .string.h In the library memcpy() and memmove() Function introduction :
You cannot assign the value of one array directly to another array , therefore , We use a loop to copy the elements of an array one by one to another array . One exception is : have access to strcpy() and strncpy() Function to copy a character array .memcpy() and memmove() Function provides a similar convenience tool for copying other types of arrays , Here are the prototypes of the two functions :
void *memcpy(void * restrict s1,const void *s2,size_t n);
void *memmove(void * s1,const void *s2,size_t n);
These two functions are from s2 Copy the location pointed to n Bytes of data to s1 Point to . And all return s1 Value . The difference between the two is determined by keywords restrict cause , namely memcpy() It can be assumed that there is no overlap between the two memory regions .memmove() Will not make this assumption , therefore , The copy process is similar to copying all bytes to a temporary buffer first , Then copy to the final destination . Use if two areas overlap memcpy() What will happen? ? The result is unknown . Therefore use memcpy() There must be no overlapping areas . but memmove() There can be overlaps .
These two functions can operate on any data type , Therefore, the two pointer parameters are void Pointer to type .C It is allowed to assign any type of pointer to void* Pointer to type . Accepting various types of pointers makes it impossible to know the type of data to be copied . therefore , These two functions use the third parameter to specify the number of bytes copied . Be careful , For arrays , The number of bytes is not equal to the number of elements . So copy 10 individual double Type data , that n=10*sizeof(double);
An example is as follows :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 10 // Define the length of array data as 10
void show_array(const int ar[], int n); // Declare array display function
int main()
{
int values[SIZE] = {
1,2,3,4,5,6,7,8,9,10 }; // Definition int Type primitive array
float test_float[SIZE]= {
1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0}; // Definition float Type primitive array
double test_double[SIZE / 2] = {
1.0,2.0,3.0,4.0,5.0 };// Definition double Type primitive array
int target[SIZE]; // Define the target array cache
puts("values(original data): ");
show_array(values, SIZE); // Output the original value
puts("target(copy data): ");
memcpy(target, values, 10 * sizeof(int));
show_array(target, SIZE); // Output the copied value , Output int form
puts("using memmove() with overlapping ranges: ");
memmove(values + 2, values, 5 * sizeof(int));
show_array(values, SIZE); // Output usage memmove() There are overwritten values after the move
puts("using memcpy() to copy float to int: ");
memmove(target, test_float, SIZE * sizeof(int));
show_array(target, SIZE); // Output the copied value , Press int Form read
puts("using memcpy() to copy float to int,but output as float: ");
printf("%f %f %f %f %f %f %f %f %f %f\n", *(float*)target, *(float*)(target + 1), *(float*)(target + 2), *(float*)(target + 3), *(float*)(target + 4), *(float*)(target + 5), *(float*)(target + 6), *(float*)(target + 7), *(float*)(target + 8), *(float*)(target + 9));// Output the copied value , Press float Form read
puts("using memcpy() to copy double to int,but output as double: ");
memmove(target, test_double, (SIZE / 2) * sizeof(double));
printf("%lf %lf %lf %lf %lf\n", *(double*)target, *(double*)(target + 2), *(double*)(target + 4), *(double*)(target + 6), *(double*)(target + 8));// Output the copied value , Press double Form read
system("pause");
return 0;
}
void show_array(const int ar[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", ar[i]);
putchar('\n');
}
Output results :
values(original data):
1 2 3 4 5 6 7 8 9 10 // Output raw data
target(copy data):
1 2 3 4 5 6 7 8 9 10 // Output replication data
using memmove() with overlapping ranges:
1 2 1 2 3 4 5 8 9 10 // The output shift covers the data
using memcpy() to copy float to int:
1065353216 1073741824 1077936128 1082130432 1084227584 1086324736 1088421888 109
0519040 1091567616 1092616192 //float to int And press int Read out data
using memcpy() to copy float to int,but output as float:
1.000000 2.000000 3.000000 4.000000 5.000000 6.000000 7.000000 8.000000 9.000000
10.000000 //float to int And press float Read out data
using memcpy() to copy double to int,but output as double:
1.000000 2.000000 3.000000 4.000000 5.000000 //double to int And press double Read out data
Two . Analysis and summary
It can be seen that , The last time memcpy() Call to transfer data from double Array copied to int Array . This shows that memcpy() Don't know or care about data types ; They just copy some bytes from one location to another ( for example , You can copy bytes from a structure to a character array ), There is no data conversion in the replication process . If you use a loop to assign values to elements one by one , Then in the assignment process, it will double The type value is converted to int Type values . Regarding this , We can figure out memcpy() and memmove() Function copies bytes as is , Then explain it in different ways according to the program , For example, you can press int Or float Type , Different display results will be obtained . Therefore, these two functions only do byte transfer , No type conversion .
边栏推荐
- Picture slide effect
- 哈希表的相关知识点
- Here is a super practical excel shortcut set (common + summary of eight categories)
- 11_ UE4 advanced_ Change male characters to female characters and modify the animation
- 零代码 | 轻松实现数据仓库建模,搭建BI看板
- 一文学会如何做电商数据分析(附运营分析指标框架)
- Network file system service (NFS)
- Tree shaking and DCE
- Ten questions about low code: tell everything about low code!
- Make a virtual human with zego avatar | virtual anchor live broadcast solution
猜你喜欢
Advanced C language: pointer (1)

分体式测斜探头安装要点及注意事项

Reading these six books makes learning MySQL easier

JS - 修改数组中对象的键名

Sword finger offer 30. stack containing min function

产品端数据分析思维

Advance.ai sailing guide helps enterprises sail to Indonesia and grasp half of the Southeast Asian market

PHP发送移动MAS短信乱码的解决方法

ctf技能树----文件上传

GKConstantNoiseSource
随机推荐
clo*******e:项目管理随记
samba服务器配置
剑指 Offer 30. 包含min函数的栈
nodejs:搭建express 服务,设置session以及实现退出操作
Invalid ROM Table原因及解决办法
Advanced C language: pointer (1)
Blue Bridge Cup embedded Hal library systick
GKRandom
Problems needing attention when VC links static libraries
nodejs:检测并安装npm模块,如果已安装则跳过
GKBillowNoiseSource
Network file system service (NFS)
Yan reported an error: could not find any valid local directory for nmprivate/
I don't know how lucky the boy who randomly typed logs is. There must be a lot of overtime
MySQL Architecture Principle
Inventory: exciting data visualization chart
Aike AI frontier promotion (7.28)
Jianzhi offer 09. realize queue with two stacks
JDBC各个类的解释
Start from scratch blazor server (2) -- consolidate databases