Preface: Hello everyone, this is YY ; this blog is mainly about the knowledge of memory functions ; including [ memcpy ] [ memmove ] [ m emcmp ] [ memset ]
PS: It is not easy to create, each knowledge point has examples or pictures to help you understand; if it is helpful to you, I hope to get your attention, like, favorite , thank you!
Table of contents
1. Overview of memory functions
1. Header file of memory function
Two. memcpy (memory copy function)
1. Simulation implementation of memcpy
2. Scenario 1: Copy a part of yourself
3. memmove (memory copy function that handles overlapping memory)
4. memcmp (memory comparison function)
Five. memset (memory setting function)
1. Overview of memory functions
1. Header file of memory function
#include<string.h>
Two. memcpy (memory copy function)
Features: limit the number of bytes , not limit the type
Application scenario:
1. Simulation implementation of memcpy
2. Scenario 1: Copy a part of yourself
PS: If you don't want to classify and discuss, use the memmove function directly
Analysis: There are two scenarios
- Destination area in front , copy from source area to back
- The target area is at the back , copy forward from the source area
Principle: Ensure that the overlapping parts can be copied from this direction first
Principle diagram:
Immediate Situation Classification:
Code display:
3. memmove (memory copy function that handles overlapping memory)
Features: memmove does not need to consider the positional relationship between the header address of the source area and the header address of the target area like memcpy
4. memcmp (memory comparison function)
The return value of memcmp:
scenes to be used:
1. Compare memcmp and strcmp
strcmp limits the character pointer type (char*) , while memcmp does not limit the type (void*)
Five. memset (memory setting function)
PS: value is the set value , num is the number of modified first num characters (number of bytes)
num assignment rules:
- To parameterize as a binary fill
scenes to be used:
PS: an int array, one element has 4 bytes;
Modify 8 bytes/9 bytes here, and change the element to 0.