In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article Xiaobian for you to introduce in detail "C language how to achieve memcpy and memmove functions", the content is detailed, the steps are clear, the details are handled properly, I hope this "C language how to achieve memcpy and memmove functions" article can help you solve your doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.
First, the introduction of memcpy function 1. Declaration of function
Void * memcpy (void * destination, const void * source, size_t num)
two。 Function function and matters needing attention
The function memcpy copies num bytes of data back from the location of source to the memory location of destination.
Note that this function does not stop when it encounters'\ 0'.
If there is any overlap between source and destination, the result of replication is undefined.
The memcpy function can copy any type of data, unlike the strcpy function, which can only copy strings.
3. Use of the function # include # include / / remember to reference its header file int main () {int arr1 [10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int arr2 [5] = {0}; / / the total size is 20 bytes memcpy (arr1, arr2, 20 bytes / copy 20 bytes of data) / / copy data from arr2 to arr1 int I = 0; printf ("data in copied arr1 is:"); for (I = 0; I
< 10; i++) { printf("%d ", arr1[i]); } return 0;} 运行结果:Second, simulate and realize the memcpy function 1. Simulation analysis
1. Because we don't know what type of data we want to copy, it could be char type data, it could be int type data, or it could be double type data. These different types of data have different sizes. In order to implement a memcpy function that can copy all types of data, we can only copy one byte at a time, because the size of the minimum type is one byte, so we can copy all types of data.
two。 Because we don't know what type of address is passed to the memcpy function, we use a pointer of type void* when we receive the passed address.
3. Since we only need to copy the data stored in the source address to the destination address, we only need to change the content stored at the destination address, not the address stored at the source address. So we need to use a pointer of type const void* to receive the source address.
4. To achieve chained access, we need to return the incoming destination starting address (destination). Since this function changes the contents of the destination store when executed, we need to recreate a pointer of type void* to store this address.
5. To avoid passing an address with a null pointer, we need to use assert to assert that the incoming address is not a null pointer.
two。 Simulation implementation # include#include// Simulation implementation memcpyvoid* my_memcpy (void* dest, const void* scr, size_t count) {assert (dest & & scr); / / asserts that the incoming address is not a null pointer void* ret = dest;// saves the destination starting address while (count--) / / copies the data stored in the source address {* (char*) dest = * (char*) scr (char*) dest = (char*) dest + 1; (char*) scr = (char*) scr + 1;} return ret;// returns the destination starting address} / / apply the simulated function int main () {int arr1 [] = {6,6,6,6,6,6} Int arr2 [] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; my_memcpy (arr2, arr1, 24); / / copy 6 bytes of data int I = 0; for (I = 0; I
< 10; i++) { printf("%d ", arr2[i]); } return 0;} 运行结果: 三、memmove函数的介绍1.函数的声明 void * memmove ( void * destination, const void * source, size_t num ); 2.为什么会有memmove函数 为什么会有memmove这个函数呢,这个还要从上面的memcpy函数说起。因为memcpy函数不能将一个数组的中的数据拷贝到自身(也就是目标数据是自己,源数据也是自己,只不过是一个数组里面不同的位置的数据拷贝到另外一个位置上),如果像这样拷贝就会出现重叠拷贝,会导致结果不是我们预期的结果。 就像下面这个代码: //应用模拟实现的memcpy函数int main(){ int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; my_memcpy(arr + 2, arr, 24);//预期出现结果为1 2 1 2 3 4 5 6 9 10 int i = 0; for (i = 0; i < 10; i++) { printf("%d ", arr[i]);//实际出现结果 } return 0;} 运行结果:The reasons why the expected results are different from the actual results are:
The reason for this result is that there are overlapping copies when the memcpy function copies its own data to its own different locations. The starting address of the source data is arr, and the starting address of the destination data is arr + 2. When we enter the memcpy function, we copy the data at arr to arr + 2 and the data at arr + 1 to arr + 3. When we want to copy the data at arr + 2 to arr + 4, we find that the data at arr + 2 has been replaced with the data at arr (1). So we can only copy 1 to arr + 4. When we want to copy the data at arr + 3 to arr + 5, we find that the data at arr + 3 has already been replaced with the data at arr + 1 (2), so we can only copy 2 to arr + 5, just like this repeated overlapping copy, the copied data has always been 1 pound, 2pm, 2x2, 1hip 2, until we have copied the number of bytes we want to copy.
So in order to copy their own data to their own different locations, we need to use the memmove function to implement, memmove function is created to solve the above problem.
3. Function function and matters needing attention
The difference between memmove and memcpy is that the source and target memory blocks handled by the memmove function can overlap.
If the source and target spaces overlap, you have to use the memmove function to handle it.
4. When using the memmove function # include#include// remember to reference its header file int main () {int arr [] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; memmove (arr + 2, arr, 24); / / the expected result is 1 2 1 2 3 4 5 6 9 10 int I = 0; for (I = 0; I < 10) ITunes +) {printf ("% d", arr [I]); / / actual result} return 0;}
This time we find that the expected result copied by the memmove function is the same as the actual result, so let's talk about the simulation implementation of the memmove function.
Fourth, simulate and realize the memmove function 1. Simulation analysis
1. The method of passing the address into the function and receiving the address is the same as the above memcpy function, and the memcpy function needs to pay attention to the memmove function, so we won't repeat it here.
Another thing to note about the 2.memmove function is that you need to analyze how to copy so as not to overlap. The following is an illustration:
Case 1: address with dest less than or equal to src
Copy from the back to the back as below so that there is no overlap.
Case 2: the address where dest is greater than scr
Copy from back to front as below so that there is no overlap.
two。 Simulation implementation # include#include// Simulation implementation memmovevoid* my_memmove (void* dest, const void* scr, size_t count) {assert (dest & & scr); / / asserts that the incoming address is not a null pointer void* ret = dest; / / saves the destination starting address if (dest
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.