memset
来源:互联网 发布:淘宝上的冬木古雨 编辑:程序博客网 时间:2024/06/05 11:28
std::memset
Defined in header <cstring> (‘<”>’的转义为
<
and>
)
Cppreference
void* memset( void* dest, int ch, std::size_t count);
Converts the value ch to
unsigned char
and copies it intoeach of the first count characters of the object pointed to by dest.Parameters
dest - pointer to the object to fill
ch - fill byte
count - number of bytes to fillReturn value
dest
Example
#include<iostream>#include<cstring>int main() { int a[20]; std::memset(a,0,sizeof a); for(int ai : a) std::cout<<ai;}output : 00000000000000000000
用法
- 常用于内存空间初始化
char a[50];memset(a,'c',50);memset(a,'/0',sizeof(a));// 初始化int数组有所不同int a[20];memset(a,0,sizeof(a)); // or memset(a,-1,sizeof(a));// 对于int数组,不能将其初始化为'0' '-1'以外的其他值// (除非该值高字节和低字节相同)// 因为memset是按字节对内存块进行初始化
- 方便地清空一个结构类型的变量或数组
struct my_struct test;memset(&test,0,sizeof(struct my_struct));struct my_struct Test[10];memset(Test,0,sizeof(struct my_struct)*10);
Tips: 当静态数组作为参数传入某个函数时,就会退化为指针,即该数组的首地址,会丢失其长度信息。所以数组作参数传递时要同时传入数组长度 或 直接用引用&
References
- 详见百度百科—memset 及 cppreference
- BLOG1
- BLOG2
0 0
- memset
- memset
- memset
- memset
- memset
- memset
- memset
- memset
- memset
- memset
- memset
- memset
- memset
- memset
- memset
- memset
- memset
- memset
- 设计模式之工厂模式
- 软件设计的三个阶段及快速构建原型
- android进程通信IPC机制之Binder详解
- matlab串口采集数据
- hdu3791--二叉搜索树
- memset
- 杭电oj(java版)——1090 A+B for Input-Output Practice (II)
- Android Apk重签名教程
- C语言复习笔记 8
- Java同步工具类——闭锁
- 重定位的理解
- 整理一下当年的学习笔记之:ajax请求json和普通项
- MAC下org.apache.jsp.index_jsp和No output folder的解决方案
- 用 JLINK和自带STLINK给STM32F429 Discovery 烧录程序