memmove

  • Move block of memory
  • Copies the values of num bytes from the location pointed by source to the memory block pointed by destination. Copying takes place as if an intermediate buffer were used, allowing the destination and source to overlap.
  • 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.
  • The function does not check for any terminating null character in source - it always copies exactly num bytes.
  • To avoid overflows, the size of the arrays pointed by both the destination and source parameters, shall be at least num bytes.
  • 从 source 所指向的对象复制 num 个字节到 destination 所指向的对象。两个对象都被转译成 unsigned char 的数组。对象可以重叠:如同复制字符到临时数组,再从该数组到 destination 一般发生复制。
  • 从 source 复制 num 个字符到 destination,但是在重叠内存块这方面,memmove() 是比 memcpy() 更安全的方法。如果目标区域和源区域有重叠的话,memmove() 能够保证源串在被覆盖之前将重叠区域的字节拷贝到目标区域中,复制后源区域的内容会被更改。如果目标区域与源区域没有重叠,则和 memcpy() 函数功能相同。
  • 若出现 destination 数组末尾后的访问则行为未定义。
  • 若 destination 或 source 为空指针则行为未定义。
void * memmove ( void * destination, const void * source, size_t num );

Parameters

destination

  • Pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*.
  • 指向用于存储复制内容的目标数组,类型强制转换为 void* 指针。
  • 指向复制目的对象的指针

source

  • Pointer to the source of data to be copied, type-casted to a pointer of type const void*.
  • 指向要复制的数据源,类型强制转换为 void* 指针。
  • 指向复制来源对象的指针

num

  • Number of bytes to copy.size_t is an unsigned integral type.
  • 要被复制的字节数。
  • 要复制的字节数

Return Value

  • destination is returned.
  • 该函数返回一个指向目标存储区 destination 的指针。
  • 返回 destination 的副本,本质为更底层操作的临时内存地址,在实际操作中不建议直接使用此地址,操作完成以后,真正有意义的地址是destination本身。

Example

//
// Created by zhangrongxiang on 2018/2/10.
//
#include <string.h>
#include <stdio.h>
#include <stdlib.h> int main() {
char str[32] = "I am your GOD";
char str2[32] = "Hello World";
char str3[] = "void * memmove ( void * destination, const void * source, size_t num );";
memmove(str2, str, strlen(str));
printf("%s\n", str2);//I am your GOD
memmove(str2, "hi\0hi", sizeof(str2));
printf("%s\n", str2);//hi
printf("%c%c%c%c\n", str2[3], str2[4], str2[5], str2[6]);//hi\0\0
memmove(str, str3, sizeof(str) - 1);
for (int i = 0; i < sizeof(str); ++i) {
printf("%c", str[i]);
}//void * memmove ( void * destina%
/////////////////////////////////////////////////////////////////////
char str4[] = "1234567890";
puts(str4);//1234567890
memmove(str4 + 4, str4 + 3, 3); // 从 [4,5,6] 复制到 [5,6,7]
puts(str4);//1234456890
////////////////////////////////////////////////////////////////////
// 设置分配的内存的有效类型为 int
int *p = malloc(3 * sizeof(int)); // 分配的内存无有效类型
int arr[3] = {1, 2, 3};
memmove(p, arr, 3 * sizeof(int)); // 分配的内存现在拥有有效类型
printf("%d%d%d\n", p[0], p[1], p[2]);//123
printf("%d%d%d\n", *p, *(p + 1), *(p + 2));//123
return 0;
}

文章参考

转载注明出处

C 标准库 - string.h之memmove使用的更多相关文章

  1. C 标准库 - string.h

    C 标准库 - string.h This header file defines several functions to manipulate C strings and arrays. stri ...

  2. C标准库<string.h>实现

    本文地址:http://www.cnblogs.com/archimedes/p/c-library-string.html,转载请注明源地址. 1.背景知识 <string.h>中声明的 ...

  3. C 标准库 - string.h之memcpy使用

    memcpy Copy block of memory Copies the values of num bytes from the location pointed to by source di ...

  4. C标准库string.h中几个常用函数的使用详解

    strlen 计算字符串长度 size_t strlen(const char *str) 计算字符串 str 的长度,直到空结束字符,但不包括空结束字符. 函数实现: int Strlen(cons ...

  5. C 标准库 - string.h之memcmp使用

    memcmp Compare two blocks of memory. Compares the first num bytes of the block of memory pointed by ...

  6. C 标准库 - string.h之memchr使用

    memchr Locate character in block of memory,Searches within the first num bytes of the block of memor ...

  7. C 标准库 - string.h之strlen使用

    strlen Returns the length of the C string str. The length of a C string is determined by the termina ...

  8. C 标准库 - string.h之strpbrk使用

    strpbrk Locate characters in string,Returns a pointer to the first occurrence in str1 of any of the ...

  9. C 标准库 - string.h之strrchr使用

    strrchr Locate last occurrence of character in string, Returns a pointer to the last occurrence of c ...

随机推荐

  1. 自我介绍和Github初次使用心得

    姓名:许洪科 班级:网络工程142 学号:1413042047 爱好:足球 编写过的程序:0 编写过的代码:几乎为0(大多数为网上直接复制的) Github注册过程:. 1:进入Github网站后点击 ...

  2. Postgres 主从配置(五)

    PostgreSQL 9.4 新增的一个特性, replication slot,  1. 可以被流复制的sender节点用于自动识别它xlog中的数据在下面的standby中是否还需要(例如, st ...

  3. Linux Socket - 内核非阻塞功能

    select 函数 int select(int maxfdp,fd_set *readfds,fd_set *writefds,fd_set *errorfds,struct timeval*tim ...

  4. Hibernate 零配置之Annotation注解

    JPA规范推荐使用Annotation来管理实体类与数据表之间的映射关系,从而避免同时维护两份文件(Java 实体类 和 XML 映射文件),将映射信息(写在Annotation中)与实体类集中在一起 ...

  5. 解决IE8不支持console

    解决IE8不支持console,代码中包含console时会报错. //解决 IE8 不支持console window.console = window.console || (function ( ...

  6. WebBrowser控件支持WebSocket

    修改html页面,在Header标签中添加如下标签: <meta http-equiv="X-UA-Compatible"content="IE=edge" ...

  7. python网络编程--协程

      1.协程 协程:是单线程下的并发,又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程,即协程是由用户程序自己控制调度的.. 需要强调的是: 1. pyt ...

  8. java学习笔记—web计算器(36)

    MVC模式 模式主要的任务是帮助开发者解决一类问题. MVC模式主要是用于规划你的网站的开发的一个基本的结构. Servlet记住充当的是控制器层.cn.itcast.controller Java类 ...

  9. C# Winform 小技巧(Datagridview某一列按状态显示不同图片)

    步骤: 一.导入状态图片到项目中: 二.在窗体中声明一个图片数组,并在窗体的OnLoad事件中加入图片资源: /// <summary> /// 存储状态图片序列,避免同一状态对图片重复读 ...

  10. 牛客OI赛制测试赛1 题解

    A 斐波那契 数竞生:这不是送分的常识吗? 这里引入一个叫卡西尼恒等式的玩意. 公式表达就是 设$fib[i]$为斐波那契数列的第$i$项$(i>0,i \in N_+)$ 则有 $fib[i+ ...