当前位置:网站首页>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 .
边栏推荐
- JSON preliminary understanding
- 构建快捷开发IDE:VisualSVN+Sublime+Visual Studio 2013+QuickEasyFTPServer
- Ten questions about low code: tell everything about low code!
- 适合中小企业的进销存软件,搞定5大难题
- Aike AI frontier promotion (7.28)
- Sword finger offer 06. print linked list from end to end
- nodejs:搭建express 服务,设置session以及实现退出操作
- Yan reports an error: exception message: /bin/bash: line 0: fg: no job control
- 哈希表的相关知识点
- Tree Shaking和DCE
猜你喜欢

产品端数据分析思维

一文学会如何做电商数据分析(附运营分析指标框架)

Sword finger offer 06. print linked list from end to end

11_ UE4 advanced_ Change male characters to female characters and modify the animation

盘点:144个免费学习网站,全网最全资源合集

Learn how to do e-commerce data analysis (with operation analysis index framework)

哈希表的相关知识点

19. Delete the penultimate node of the linked list

Table data processing software, what else besides excel?

做数据分析,你还不懂RFM分析方法(模型)?
随机推荐
Yan reports an error: exception message: /bin/bash: line 0: fg: no job control
1. Sum of two numbers
Zero code | easily realize data warehouse modeling and build Bi Kanban
11_ UE4 advanced_ Change male characters to female characters and modify the animation
GKRandom
Nodejs: mongodb simple fuzzy + paging query instance
Development environment configuration of nodemcu
Reading these six books makes learning MySQL easier
Nodejs: return value of mongodb after successful insertion
吊打面试官的问题
分体式测斜探头安装要点及注意事项
Learn how to do e-commerce data analysis (with operation analysis index framework)
蓝桥杯嵌入式-HAL库-USART_RX
适合中小企业的进销存软件,搞定5大难题
Do you want to enroll in class for advanced soft exam
GKRidgedNoiseSource
GKSpheresNoiseSource
Learn these analysis methods and models, and no longer have no ideas when encountering problems
这里有一份超实用Excel快捷键合集(常用+八大类汇总)
Sword finger offer 30. stack containing min function