当前位置:网站首页>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))
边栏推荐
- EasyExcel的读写操作
- Wood board ISO 5660-1 heat release rate mapping test
- Pytorch实战——MNIST数据集手写数字识别
- 854. 相似度为 K 的字符串 BFS
- [daily training -- Tencent select 50] 89 Gray code (only after seeing the solution of the problem)
- Problems encountered in office--
- 秋招将临 如何准备算法面试、回答算法面试题
- Clion-MinGW编译后的exe文件添加ico图标
- Pytoch practice -- MNIST dataset handwritten digit recognition
- The primary key is set after the table is created, but auto increment is not set
猜你喜欢

uni-app 蓝牙通信

CLion配置visual studio(msvc)和JOM多核编译

Introduction of ArcGIS grid resampling method

KingbaseES V8R3集群维护案例之---在线添加备库管理节点

EasyExcel的讀寫操作

使用Aspect制作全局异常处理类

Making global exception handling classes with aspect

How to prepare for the algorithm interview and answer the algorithm interview questions

Two ways to realize video recording based on avfoundation

冯唐“春风十里不如你”数字藏品,7月8日登录希壤!
随机推荐
Ethereum ETH的奖励机制
xlrd常见操作
Objects in the list, sorted by a field
递归查询多级菜单数据
Pytoch practice -- MNIST dataset handwritten digit recognition
JS common method encapsulation
2.2.5 basic sentences of R language drawing
Introduction of ArcGIS grid resampling method
股票开户选择哪家证券公司比较好哪家平台更安全
Recursive query of multi-level menu data
【案例】定位的运用-淘宝轮播图
Dictionary tree simple introductory question (actually blue question?)
1.2 download and installation of the help software rstudio
Cross end solution to improve development efficiency rapidly
MySQL deep paging optimization with tens of millions of data, and online failure is rejected!
@Validated basic parameter verification, grouping parameter verification and nested parameter verification
[daily training] 729 My schedule I
854. 相似度为 K 的字符串 BFS
Which securities company is better and which platform is safer for stock account opening
postgis 安装地理信息扩展