1.memcpy()
1.1 INTRODUCTION
Header file: #include < string h>
memcpy() is used to copy memory data. Its prototype is:
void * memcpy ( void * dest, const void * src, size_t num );
memcpy() will copy the first num bytes of the memory content indicated by src to the memory address indicated by dest.
Because the passed parameters are received with void *, it does not care about type parameters. It can pass any type and copy it byte by byte, which brings great flexibility to the use of functions and can be copied for any data type.
Note that:
- The dest pointer should allocate enough space, that is, space greater than or equal to num bytes. If no space is allocated, a memory overflow error occurs.
- The memory space referred to by dest and src cannot overlap (if overlap occurs, part of the data will be overwritten).
And strcpy() "\ meMy" is different from "num", because the copy will end with "\ meMy" bytes instead of "\ num".
[return value] returns a pointer to dest. Note that the returned pointer type is void, and forced type conversion is generally required when using.
1.2 the simulation is realized as follows:
typedef struct Person { char name[10]; int age; }Person; //size_t is the system custom type unsigned int void* my_memcpy(void* desc,const void* src ,size_t num) { //The assrt() function determines whether the address passed is null assert(desc); assert(src); //One byte one byte assignment while (num--) { *(char*)desc = *(char*)src; ++(char*)desc; ++(char*)src; } return desc; } int main() { Person p1[] = { {"jack",12},{"lucy",22},{"mary",34} }; Person p2[3]; my_memcpy(p2, p1, sizeof(p1)); int length = sizeof(p2) / sizeof(p2[0]); printf("p1 For:\n"); for (int i = 0; i < length; i++) { printf("%s %d\n", p1[i].name, p1[i].age); } printf("p2 For:\n"); for (int i = 0; i < length; i++) { printf("%s %d\n", p2[i].name,p2[i].age); } }
1.3 test results
P1 and P2 are the same, and the copy is successful
2.memmove()
2.1 introduction
Function prototype
void* memmove( void* dest, const void* src, std::size_t count ); |
Copy # count bytes from the object pointed to by # src # to the object pointed to by # dest #. Both objects are translated into an array of unsigned char.
It is similar to copying characters to a temporary array, and then copying from the array to {dest}.
parameter
dest | - | Pointer to the memory location of the copy destination |
src | - | Pointer to the memory location of the copy source |
count | - | Number of bytes to copy |
Return value
dest
Pointer to the memory location of the copy destination |
2.2 difference from memcpy()
The only difference between them is that when the memory overlaps locally, memmove can ensure the correct copy, and the result of memcpy copy is undefined (depending on the optimization of memcpy in the compilation platform).
2.3 simulation implementation memmove()
void* my_memmove(void* desc, const void* src, size_t count) { assert(desc != NULL); assert(src != NULL); void* res = desc; if (desc > src) { while (count--) *((char*)desc +count) = *((char*)src +count); } else { while (count--) { *(char*)desc = *(char*)src; ++(char*)desc; ++(char*)src; } } return res; } int main() { int arr[] = { 1,2,3,4,5,6,7,8,9,10 }; int length = sizeof(arr) / sizeof(arr[0]); size_t validBit = sizeof(arr[0]) * 5; my_memmove(arr + 4, arr, validBit); for (int i = 0; i < length; i++) { printf("%d ", arr[i]); } }
2.4 testing