当前位置:网站首页>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))
边栏推荐
- 递归查询多级菜单数据
- Environment configuration problem record
- Teach yourself to train pytorch model to Caffe (I)
- Deep merge object deep copy of vant source code parsing
- Add ICO icon to clion MinGW compiled EXE file
- Vant source code parsing event Detailed explanation of TS event processing global function addeventlistener
- 字典树简单入门题(居然是蓝题?)
- Get JS of the previous day (timestamp conversion)
- 基于vertx-web-sstore-redis的改造实现vertx http应用的分布式session
- What should I do to prepare for the interview algorithm position during school recruitment?
猜你喜欢
Clickhouse copy paste multi line SQL statement error
【案例】定位的运用-淘宝轮播图
"Grain mall" -- Summary and induction
Clion-MinGW编译后的exe文件添加ico图标
EasyExcel的读写操作
Some common processing problems of structural equation model Amos software
基于vertx-web-sstore-redis的改造实现vertx http应用的分布式session
阿里云有奖体验:用PolarDB-X搭建一个高可用系统
第05章_存储引擎
Cold violence -- another perspective of objective function setting
随机推荐
XML modeling
SQL common syntax records
LeetCode_ Hash table_ Difficulties_ 149. Maximum number of points on the line
Realize the function of verifying whether the user has completed login when browsing the page
Making global exception handling classes with aspect
Aitm 2-0003 horizontal combustion test
Using webassembly to operate excel on the browser side
LeetCode_哈希表_困难_149. 直线上最多的点数
854. 相似度为 K 的字符串 BFS
Reading and writing operations of easyexcel
ESP32
Simple interest mode - evil Chinese style
Uni app Bluetooth communication
selenium 获取dom内属性值的方法
Cold violence -- another perspective of objective function setting
Teach yourself to train pytorch model to Caffe (I)
sql常用语法记录
【日常训练】729. 我的日程安排表 I
Emotional analysis of wechat chat records on Valentine's day based on Text Mining
The transformation based on vertx web sstore redis to realize the distributed session of vertx HTTP application