当前位置:网站首页>Detailed explanation of memset() function usage
Detailed explanation of memset() function usage
2022-07-05 21:35:00 【MrL_ JJ】
List of articles
One 、memset The function prototype
void *memset(void *src, int value, size_t n); here srs It can be array names , It can also be a pointer to a memory space ;
value For the values to be filled ;
n For the number of bytes to be filled , Usually it is sizeof(s);
Function functions : Put the pointer variable src Front of the point n Byte memory unit with a “ Integers ” value Replace , Be careful value yes int type .src yes void* Pointer variable of type , So it can be initialized for any type of data .
Two 、 Use steps
1. Add header files and namespaces
The code is as follows ( Example ):
#include <string.h>
using namespace std;
2. Array initialization
The code is as follows ( Example ):
char str[20];
char *pt = str;
memset(pt, 0, sizeof(str)); // take str The array is initialized to 0
3. Custom class initialization
In our custom class or structure , It usually defines int,char,double etc. Built in variable , Initialize them sentence by sentence in the constructor as 0 It seems very troublesome , So you can directly memset(this, 0, sizeof *this); Set the memory of the entire object to 0. It can work well in this situation , But the following situations can't be used like this :1. Class contains a table of virtual functions : Will destroy the virtual function table , Exceptions will appear when calling virtual functions later ;
2. Class contains C++ Object of type : for example , One is defined in the class vector The object of , Because before the code of the constructor body is executed vector Object is initialized , hypothesis vector It allocates memory in its constructor , So we're going to destroy it vector Object's memory .
The code is as follows ( Example ):
memset(this,0,sizeof(*this))
边栏推荐
- [case] Application of element display and hiding -- element mask
- Wood board ISO 5660-1 heat release rate mapping test
- Chap2 steps into the palace of R language
- How to send samples when applying for BS 476-7 display? Is it the same as the display??
- Explain various hot issues of Technology (SLB, redis, mysql, Kafka, Clickhouse) in detail from the architecture
- Realize the function of verifying whether the user has completed login when browsing the page
- Sophomore personal development summary
- 终端安全能力验证环境搭建和渗透测试记录
- Talk about my fate with some programming languages
- Selenium's method of getting attribute values in DOM
猜你喜欢
Parker driver maintenance COMPAX controller maintenance cpx0200h
LeetCode_哈希表_困难_149. 直线上最多的点数
阿里云有奖体验:用PolarDB-X搭建一个高可用系统
Chapter 05_ Storage engine
張麗俊:穿透不確定性要靠四個“不變”
MMAP
Two ways to realize video recording based on avfoundation
xlrd常见操作
Deployment of Jenkins under win7
Opérations de lecture et d'écriture pour easyexcel
随机推荐
字典树简单入门题(居然是蓝题?)
vant 源码解析之 utils/index.ts 工具函数
How to prepare for the algorithm interview and answer the algorithm interview questions
有些事情让感情无处安放
2022-07-03-cka- latest feedback from fans
Deployment of Jenkins under win7
uni-app 蓝牙通信
vant 源码解析 event.ts 事件处理 全局函数 addEventListener详解
场景化面试:关于分布式锁的十问十答
Teach yourself to train pytorch model to Caffe (I)
R语言【数据管理】
100 cases of shell programming
Zhang Lijun: penetrating uncertainty depends on four "invariants"
EasyExcel的读写操作
Longest swing sequence [greedy practice]
R language learning notes
MySQL 千万数据量深分页优化, 拒绝线上故障!
Sitge joined the opengauss open source community to jointly promote the ecological development of the database industry
vant 源码解析 之深层 合并对象 深拷贝
面试官:并发编程实战会吗?(线程控制操作详解)