当前位置:网站首页>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))
边栏推荐
- Learning robots have no way to start? Let me show you the current hot research directions of robots
- 事项研发工作流全面优化|Erda 2.2 版本如“七”而至
- Learning notes of statistical learning methods -- Chapter 1 Introduction to statistical learning methods
- Get JS of the previous day (timestamp conversion)
- The primary key is set after the table is created, but auto increment is not set
- Three components of openpyxl
- Exercise 1 simple training of R language drawing
- Aitm2-0002 12s or 60s vertical combustion test
- "Grain mall" -- Summary and induction
- SQL common syntax records
猜你喜欢

2.2.3 output of documents

让开发效率飞速提升的跨端方案

Teach yourself to train pytorch model to Caffe (2)

"Grain mall" -- Summary and induction

张丽俊:穿透不确定性要靠四个“不变”

Reading and writing operations of easyexcel

SQL knowledge leak detection

Pytorch实战——MNIST数据集手写数字识别

R language learning notes

Evolution of zhenai microservice underlying framework from open source component encapsulation to self-development
随机推荐
張麗俊:穿透不確定性要靠四個“不變”
Making global exception handling classes with aspect
Learning notes of statistical learning methods -- Chapter 1 Introduction to statistical learning methods
ESP32
Sitge joined the opengauss open source community to jointly promote the ecological development of the database industry
leetcode:1139. The largest square bounded by 1
vant 源码解析 之深层 合并对象 深拷贝
Clion configures Visual Studio (MSVC) and JOM multi-core compilation
事项研发工作流全面优化|Erda 2.2 版本如“七”而至
Robot framework setting variables
leetcode:1755. Sum of subsequences closest to the target value
EasyExcel的讀寫操作
SYSTEMd resolved enable debug log
selenium 获取dom内属性值的方法
int GetMonth( ) const throw( );后面的throw( )什么意思?
selenium 获取dom内验证码图片
Five layer network protocol
Feng Tang's "spring breeze is not as good as you" digital collection, logged into xirang on July 8!
postgis 安装地理信息扩展
Introduction of ArcGIS grid resampling method