惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
U
Unit 42
大猫的无限游戏
大猫的无限游戏
H
Help Net Security
G
Google Developers Blog
Recent Announcements
Recent Announcements
B
Blog RSS Feed
罗磊的独立博客
博客园 - Franky
J
Java Code Geeks
S
SegmentFault 最新的问题
D
DataBreaches.Net
C
Check Point Blog
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
有赞技术团队
有赞技术团队
腾讯CDC
博客园_首页
美团技术团队
V
Visual Studio Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
GbyAI
GbyAI
The Cloudflare Blog
aimingoo的专栏
aimingoo的专栏

博客园 - Lunais

恢复 git stash drop 丢失的内容 Git技巧:彻底重置本地仓库与远程同步,同时保留Stash内容 信源编码和信道编码区别 Leetcode scanf 一组数据差值之和最大 5G 3gpp协议 eclipse调试配置 find查找 浮点类型(float、double)在内存中的存储 yield python2处理表格文件按照规则多行输出(中文路径,print) linux系统objdump输出动态库.so文件和静态库.a中符号表 python复制删除文件&修改目录&执行bat(wins) Linux C下的正则表达式 kill eclipse C语言之表达式运算整体提升 查找函数对比:findall,search,match Linux backtrace() git本地协同
[基础函数]memset用法
Lunais · 2021-06-23 · via 博客园 - Lunais

这个基础函数,主要见过两种用法:

1.内存初始化

2.考试!!!(鉴于经常考试翻车,立个flag,逢考必对~)

memset声明:

void * memset ( void * ptr, int value, size_t num );

memset入参:

ptr:
  Pointer to the block of memory to fill.
value:
  Value to be set. The value is passed as an int, but the function fills the block of memory using the unsigned char conversion of this value.
num:
  Number of bytes to be set to the value.  size_t is an unsigned integral type.
由于是按照字节赋值,来看一个经典考试题
 1 #include "stdio.h"
 2 #include "string.h"
 3 
 4 int main()
 5 {
 6     int i = 0;
 7     memset(&i, 256, sizeof(i));
 8 
 9     unsigned char* m = (unsigned char*)(&i);
10     for (int j = 0; j < sizeof(i); j++)
11     {
12         printf("%d\n", m[j]);
13     }
14     return 0;
15 }

output:0000
memset(&i, 128, sizeof(i));
output:
128
128
128
128
256是0x100,取一个无符号字节是0x00
128是0x80(0b1000 0000 或者 128),取一个无符号字节是0x80