当前位置: 首页 > 编程日记 > 正文

mem 族函数的实现

1.void * memcpy ( void * dest, const void * src, size_t num );

头文件:#include <string.h>
memcpy() 用来复制内存,memcpy() 会复制 src 所指的内存内容的前 num 个字节到 dest 所指的内存地址上。
memcpy() 并不关心被复制的数据类型,只是逐字节地进行复制,这给函数的使用带来了很大的灵活性,可以面向任何数据类型进行复制。
需要注意的是:

  • dest 指针要分配足够的空间,也即大于等于 num 字节的空间。如果没有分配空间,会出现断错误。
  • dest 和 src 所指的内存空间不能重叠(如果发生了重叠,使用 memmove() 会更加安全)。
     strcpy() 不同的是,memcpy() 会完整的复制 num 个字节,不会因为遇到“\0”而结束。

【返回值】返回指向 dest 的指针。注意返回的指针类型是 void,使用时一般要进行强制类型转换。

void* Memcpy(void* dst,const void* src, int n)
{if (dst == NULL || src == NULL){return dst;}void* result = dst;while (n--){*(char*)dst = *(char*)src;dst = (char*)dst + 1;src = (char*)src + 1;}return result;
}


2.void* memmove(void* dst, const void* src, size_t n) 

但当源内存和目标内存存在重叠时,memcpy会出现错误,而memmove能正确地实施拷贝,但这也增加了一点点开销。

memmove的处理措施:

(1)当源内存的首地址等于目标内存的首地址时,不进行任何拷贝

(2)当源内存的首地址大于目标内存的首地址时,实行正向拷贝

(3)当源内存的首地址小于目标内存的首地址时,实行反向拷贝

void* Memmove(void* dst,const void* src, int n)
{if (dst == NULL || src == NULL || dst == src)   //不用拷贝
    {return dst;}char* m_dst = (char*)dst;char* m_src = (char*)src;if (m_dst > m_src && m_dst < m_src + n) //反向拷贝
    {m_dst = m_dst + n - 1;m_src = m_src + n - 1;while (n--){*m_dst-- = *m_src--;}            }else  //正向拷贝
    {while (n--){*m_dst++ = *m_src++;}        }return dst;
}

3.memcmp

头文件:#include <string.h>
定义函数:int memcmp (const void *s1, const void *s2, size_t n);
函数说明:memcmp()用来比较s1 和s2 所指的内存区间前n 个字符。
字符串大小的比较是以ASCII 码表上的顺序来决定,次顺序亦为字符的值。memcmp()首先将s1 第一个字符值减去s2 第一个字符的值,若差为0 则再继续比较下个字符,若差值不为0 则将差值返回。例如,字符串"Ac"和"ba"比较则会返回字符'A'(65)和'b'(98)的差值(-33)。
返回值:若参数s1 和s2 所指的内存内容都完全相同则返回0 值。s1 若大于s2 则返回大于0 的值。s1 若小于s2 则返回小于0 的值。

int Memcmp(const void *s1, const void *s2, size_t n)
{assert(s1 && s2);char* m_s1 = (char*)s1;char* m_s2 = (char*)s2;while (n--){if (*m_s1 - *m_s2){return *m_s1 - *m_s2;}else{m_s1++;m_s2++;}}return 0;
}

4.memset

memset() 函数用来将指定内存的前n个字节设置为特定的值,其原型为:
    void * memset( void * ptr, int value, size_t num );

参数说明:

  • ptr 为要操作的内存的指针。
  • value 为要设置的值。你既可以向 value 传递 int 类型的值,也可以传递 char 类型的值,int 和 char 可以根据 ASCII 码相互转换。
  • num 为 ptr 的前 num 个字节,size_t 就是unsigned int。


【函数说明】memset() 会将 ptr 所指的内存区域的前 num 个字节的值都设置为 value,然后返回指向 ptr 的指针。

memset() 可以将一段内存空间全部设置为特定的值,所以经常用来初始化字符数组。例如:

  1. char str[20];
  2. memset(str, '\0', sizeof(str)-1);


【返回值】返回指向 ptr 的指针。

注意:参数 value 虽声明为 int,但必须是 unsigned char,所以范围在0 到255 之间。

void * Memset(void * ptr, int value, size_t num)
{assert(ptr);char* m_ptr = (char*)ptr;while (num--){*(m_ptr++) = value;}return ptr;
}

转载于:https://www.cnblogs.com/shihaochangeworld/p/5500772.html

相关文章:

快排递归非递归python_Python递归神经网络终极指南

快排递归非递归pythonRecurrent neural networks are deep learning models that are typically used to solve time series problems. They are used in self-driving cars, high-frequency trading algorithms, and other real-world applications.循环神经网络是深度学习模型…

我的hadoop学习之路

Hadoop实现了一个分布式文件系统&#xff08;Hadoop Distributed File System&#xff09;&#xff0c;简称HDFS。HDFS有高容错性的特点&#xff0c;并且设计用来部署在低廉的&#xff08;low-cost&#xff09;硬件上。 Hadoop的框架最核心的设计就是&#xff1a;HDFS和MapRedu…

日期处理工具类 -【二】

1、返回本周的第一天(周日为每周第一天) 1 /**2 * 返回本周的第一天(周日为每周第一天)3 * return4 */5 public static String getTheFirstDayOfThisWeek(){6 SimpleDateFormat format new SimpleDateFormat("yyyy-MM-dd");7 Calendar cal Calendar.get…

超越对手pdf_如何创建一个超越竞争对手的移动应用

超越对手pdfThe amount of time people spend on their mobile phones has increased over the years, and so has the number of people using mobile devices.多年来&#xff0c;人们在手机上花费的时间增加了&#xff0c;使用移动设备的人数也增加了。 It’s safe to say t…

vue路由对象($route)参数简介

路由对象在使用了 vue-router 的应用中&#xff0c;路由对象会被注入每个组件中&#xff0c;赋值为 this.$route &#xff0c;并且当路由切换时&#xff0c;路由对象会被更新。 so , 路由对象暴露了以下属性&#xff1a; 1.$route.path 字符串&#xff0c;等于当前路由对象的路…

join......on 后面的and 和where的区别

a.where 是在两个表join完成后&#xff0c;再附上where条件。   b. and 则是在表连接前过滤A表或B表里面哪些记录符合连接条件&#xff0c;同时会兼顾是left join还是right join。即   假如是左连接的话&#xff0c;如果左边表的某条记录不符合连接条件&#xff0c;那么它不…

block的运用

cell的.h文件 typedef void(^ActivityCellBlock)(NSString *str); interface ActivityCell : UITableViewCell property (nonatomic,strong) NSArray *labelAry; property (nonatomic,copy) ActivityCellBlock myBlock; -(void)showCell:(ActivityCellBlock)myBlock; cel…

如何在Ubuntu 20.04上设置Python虚拟环境

I recently got myself a “new” laptop – a Lenovo x270 (yay)! And once again I needed to set up a Python virtual environment. So of course I Googled for a solution, just to find my previously written article on the same topic!我最近给自己买了一台“新”笔记…

getURLParameters - 网址参数

返回包含当前URL参数的对象。 通过适当的正则表达式&#xff0c;使用 String.match() 来获得所有的键值对&#xff0c; Array.reduce() 来映射和组合成一个单一的对象。 将 location.search 作为参数传递给当前 url。 const getURLParameters url >url.match(/([^?&])…

block的使用

#import "ViewController.h" /* 使用Block最大的一个好处就是可以在代码块中随时访问外部变量 比如你在A.class类中的某个方法中声明了一段代码块.你可以在代码块中直接对A.class所拥有的成员变量进行调用,并且,通过一定的条件(__block),还可以随时的修改这…

关于二级菜单的问题

大家在做二级菜单的时候经常会碰到鼠标移出一级菜单&#xff0c;二级菜单瞬间消失&#xff0c;根本不给你机会移到二级菜单上&#xff0c;今天分享下怎样解决这个问题。 ①第一种介绍一种简单粗暴的方法&#xff0c;二级菜单的元素放入一级菜单中。 代码地址&#xff1a;http:h…

python打印换行符_Python换行符以及如何在不使用换行符的情况下进行Python打印

python打印换行符Welcome! The new line character in Python is used to mark the end of a line and the beginning of a new line. Knowing how to use it is essential if you want to print output to the console and work with files.欢迎&#xff01; Python中的新行字…

tomcat启动后 项目运行缓慢,要几十到几百秒不等 怎么样./startup.sh 运行加快

修改 linux系统中 /usr/local/jdk1.8.0_11/jre/lib/security/java.security 借力 好文章。我们新的Linux系统&#xff0c;部署了多个 Tomca&#xff0c;同时重启后t, 每次都阻塞差不多260秒左右。修改之后总的启动时间下降到6-8秒左右。另外&#xff0c;不确定为什么&#xff0…

转载 C#中使用结构来传递多个参数

C#中当参数超过5个时&#xff0c;建议用结构来传递多个参数。 示例代码如下&#xff1a; 1 public struct MyStruct2 {3 public string str;4 public int number;5 }6 7 class Program8 {9 static void Main(string[] args) 10 { 11 MyStruct myStruct…

xmpp 开源项目选择_如何选择和维护安全的开源项目

xmpp 开源项目选择评估开源项目安全性的一些技巧。 (A few tricks for assessing the security of an open source project.) There is a rather progressive sect of the software development world called the open source community. 在软件开发领域&#xff0c;有一个相当…

【2018-01-22】HTML-表单及表单元素

<body><!--表单--><form action"" method"post"><!--文本类--><input type"text" value"" placeholder"请输入用户名"/><!--文本框--><input type"password" value"…

取消tableView多余的横线

- (void)setExtraCellLineHidden: (UITableView *)tableView{UIView *view [UIView new];view.backgroundColor [UIColor clearColor];[tableView setTableFooterView:view];}

iOS 自定义UITabBar

推荐一篇非常好的集成各种UITabBar的三方库 《点击这里直取demo》 另外一篇根据runtime定制了一款可以出轨的UITarBar 《Runtime实战之定制TabBarItem大小》 点击view的触发机制《iOS事件分发机制&#xff08;一&#xff09; hit-Testing》 摘自&#xff1a; UIView中提供两个…

react入门代码_如何在React中构建温度控制应用程序-包括提示和入门代码

react入门代码我们正在建立的 (What were building) In this beginner React project, were going to learn how to use state hooks, handle events, apply CSS based on state, and more! Check it out: 在这个初学者的React项目中&#xff0c;我们将学习如何使用状态挂钩&am…

决策树(chap3)Machine Learning In Action学习笔记

优点&#xff1a;计算复杂度不高&#xff0c;输出结果易于理解&#xff0c;对中间值的缺失不敏感&#xff0c;可以处理不相关特征数据。缺点&#xff1a;可能会产生过度匹配问题。适用数据类型&#xff1a;数值型&#xff08;必须离散化&#xff09;和标称型。决策树创建分支的…

BigdCIMAL类型数据的使用选择

现在常用的数值类型有Integer , Double , Float , BigDecimal几种 , 常用的当然要数前两种 了 , Integer代表的是整数类型的数据 , double则是代表的是浮点型 , 双精度 ,double的计算精度相对于float来讲要 高 , BigDecimal的计算精度则是最高的 . 可是BigDecimal的一些计算方…

数字字符串转化为时间字符串

(NSString *)dateStringFromNumberTimer:(NSString *)timerStr {//转化为Doubledouble t [timerStr doubleValue];//计算出距离1970的NSDateNSDate *date [NSDate dateWithTimeIntervalSince1970:t];//转化为 时间格式化字符串NSDateFormatter *df [[NSDateFormatter alloc]…

git 覆盖本地修改_Git拉力–如何使用Git覆盖本地更改

git 覆盖本地修改When you learn to code, sooner or later youll also learn about Version Control Systems. And while there are many competing tools in this space, one of them is the de facto standard used by almost everyone in the industry. Its so popular tha…

云计算大会记录

一、要点及主要技术内容记录消费金融刘志军 马上消费大额 分散 小额 短期OpenStack OpenStack是一个由NASA&#xff08;美国国家航空航天局&#xff09;和Rackspace合作研发并发起的&#xff0c;以Apache许可证授权的自由软件和开放源代码项目。 OpenStack是一个开源的云计算管…

获取iOS版本号

(double)getCurrentIOS {return [[[UIDevice currentDevice] systemVersion] doubleValue];}

spring boot 服务 正确关闭方式

引言 Spring Boot&#xff0c;作为Spring框架对“约定优先于配置(Convention Over Configuration)”理念的最佳实践的产物&#xff0c;它能帮助我们很快捷的创建出独立运行、产品级别的基于Spring框架的应用&#xff0c;大部分Spring Boot应用只需要非常少的配置就可以快速运行…

如何在5美元的Raspberry Pi上构建个人开发服务器

In this article, youll learn how to build a personal dev server by installing Git, Node.js, Rust, and Docker on a Raspberry Pi. The cheapest option costs just $5. You can get a starter kit ($25) for free here.在本文中&#xff0c;您将学习如何通过在Raspberry…

Eclipse:xml文件中添加.xsd约束文件

今天在使用dubbo的时候&#xff0c;XML文件一直报错。找不到dubbo的xsd约束文件。 cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element dubbo:reference 解决方法&#xff1a; 找到dubbo的jar包&#xff0c;然后在META-…

029 浏览器不能访问虚拟机的问题解决

1.在搭建分布式时 ssh一直不能进行scp&#xff0c;后来发现一个问题。 windows中的hosts配置了三台虚拟机的映射&#xff0c;但是在虚拟机中的hosts没有配置。 做法是在每台虚拟机上都配置三台虚拟机的映射。 2.端口访问与防火墙 最近帮别人解决问题时才注意的。 以前安装好虚拟…

获取 一个文件 在沙盒Library/Caches/ 目录下的路径

(NSString *)getFullPathWithFile:(NSString *)urlName {//先获取 沙盒中的Library/Caches/路径NSString *docPath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];NSString *myCacheDirectory [docPath stringByAppendingPat…