1 函数原型
memmove():移动内存块,函数原型如下:
void * memmove ( void * destination, const void * source, size_t num );
cstring库描述如下:
Move block of memory
1. Copies the values of num bytes from the location pointed by source to the memory block pointed by destination.
2. Copying takes place as if an intermediate buffer were used, allowing the destination and source to overlap.
3. The underlying type of the objects pointed by both the source and destination pointers are irrelevant for this function; The result is a binary copy of the data.
4. The function does not check for any terminating null character in source - it always copies exactly num bytes.
5. To avoid overflows, the size of the arrays pointed by both the destination and source parameters, shall be at least num bytes.
- memmove()函数:
(1)从source指向的内存块直接复制num个字节的值到destination指向的内存块; - memmove()函数和memcpy()函数都用于内存拷贝,区别在于:
(1)memmove()函数能够正确处理内存重叠的情况,而memcpy()函数不能。
2 参数
memmove()函数有三个参数source、destination和num:
- source是指向源内存块的指针,类型为void*;
- destination是指向目标内存块的指针,类型为void*;
- num是要复制的字符数,类型为size_t。
cstring库描述如下:
destination
1. Pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*.
source
1. Pointer to the source of data to be copied, type-casted to a pointer of type const void*.
num
1. Number of bytes to copy.
2. size_t is an unsigned integral type.
3 返回值
memmove()函数的返回值类型为void*型:
- 返回指向目标内存块的指针destination。
cstring库描述如下:
1. destination is returned.
4 示例
示例代码如下所示:
int main() {
//
char str[] = "abcdefghijklmn";
//
printf("移动前:%s\n", str);
//
memmove(str + 6, str + 4, 6);
//
printf("移动后:%s\n", str);
//
return 0;
}
代码运行结果如下图所示:
代码及运行结果分析如下:
- 期望用"efghij"替换"ghijkl",替换成功。