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

推荐订阅源

Help Net Security
Help Net Security
G
Google Developers Blog
雷峰网
雷峰网
WordPress大学
WordPress大学
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Engineering at Meta
Engineering at Meta
Security Latest
Security Latest
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
F
Full Disclosure
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
J
Java Code Geeks
U
Unit 42
C
Cyber Attacks, Cyber Crime and Cyber Security
V
V2EX
C
Cisco Blogs
博客园 - 司徒正美
Project Zero
Project Zero
L
LINUX DO - 热门话题
阮一峰的网络日志
阮一峰的网络日志
Blog — PlanetScale
Blog — PlanetScale
Scott Helme
Scott Helme
A
About on SuperTechFans
Hugging Face - Blog
Hugging Face - Blog
S
Securelist
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
S
Schneier on Security
G
GRAHAM CLULEY
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
MongoDB | Blog
MongoDB | Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
T
Threatpost
Recorded Future
Recorded Future
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
The Register - Security
The Register - Security
S
Security Archives - TechRepublic
博客园 - Franky
N
News | PayPal Newsroom
Simon Willison's Weblog
Simon Willison's Weblog
S
SegmentFault 最新的问题
W
WeLiveSecurity
A
Arctic Wolf
B
Blog

博客园 - AaronChen

程序员导师制(师傅带徒弟)指导意见 C++程序员进阶书籍推荐 单元测试 测试全攻略 2009年研发经理工作总结 人为什么活着读后感 高效研发团队管理的秘诀 用Visual C++设计屏幕抓图程序 C++总结 友元类和友元函数(转) 今天,让我们一起努力 C++测试全攻略(转) CppUnit单元测试 电信级系统性能分析 不许移动鼠标到照片上 仿银行密码输入保护器(软键盘) 如何有效地编写软件 史上最伟大的十大程序员 教你怎样快速DIY自己的博客园SKIN petshop4.0 详解之八(PetShop表示层设计)
memset、memcpy使用陷阱
AaronChen · 2011-08-16 · via 博客园 - AaronChen

void* memset(void *s,int c,size_t n)
作用:将已开辟内存空间 s 的首 n 个字节的值设为值 c。是在一段内存块中填充某个给定的值,它是对较大的结构体或数组进行清零操作的一种最快方法。

比如:

一、char strtmp[100]; memset(strtmp,0,100);

二、struct sample_struct

{
char csName[16];
int iSeq;
int iType;
};
struct sample_strcut stTest;
一般情况下,清空stTest的方法:
stTest.csName[0]='/0';
stTest.iSeq=0;
stTest.iType=0;
用memset就非常方便:

memset(&stTest,0,sizeof(struct sample_struct));

三、使用陷阱

memset函数是基于bitwise原则的,当要进行操作的对象违反此原则的话,致命的bug将出现。

比如:

class Ctest

{

public:

virtual int getsub();

private:

int a;

int b;

}

Ctest  t_a;

memset(&t_a,0,sizeof(Ctest));

这个操作将破坏t_a对象原来的内存数据,导致编译器自动产生的vtbl 值被修改掉;

memcpy也存在同样的问题;