不要对含有virtual函数的类使用memset
来源:互联网 发布:异星工厂修改数据 编辑:程序博客网 时间:2024/06/05 02:48
Using Memset On Class Objects
It's common practice in C, to do a memset on structures, in order to initialize all member variables to some default value, usually NULL. Similarly, you can use memset to initialize class objects. But what happens if the class contains some virtual functions?
Let's take an example:
This results in a crash, because every object of a class containing virtual functions contains a pointer to the virtual function table(vtbl). This pointer is used to resolve virtual function calls, at run time and for dynamic type casting. The pointer is hidden, and is not accessible to programmers by normal means. When we do a memset, the value of this pointer also gets overwritten, which, in turn, results in a crash, if a virtual function is called.
To avoid such mysterious crashes, memset should not be used on the objects of a class with virtual functions. Instead use the default constructor or an init routine to initialize member variables.
It's common practice in C, to do a memset on structures, in order to initialize all member variables to some default value, usually NULL. Similarly, you can use memset to initialize class objects. But what happens if the class contains some virtual functions?
Let's take an example:
class GraphicsObject{protected:char *m_pcName;int m_iId;//etcpublic:virtual void Draw() {}virtual int Area() {}char* Name() { return m_pcName;}};class Circle: public GraphicsObject{void Draw() { /*draw something*/ }int Area() { /*calculate Area*/ }};void main(){GraphicsObject *obj = new Circle; //Create an objectmemset((void *)obj,NULL,sizeof(Circle)); //memset to 0obj->Name(); //Non virtual function call works fineobj->Draw(); //Crashes here}
This results in a crash, because every object of a class containing virtual functions contains a pointer to the virtual function table(vtbl). This pointer is used to resolve virtual function calls, at run time and for dynamic type casting. The pointer is hidden, and is not accessible to programmers by normal means. When we do a memset, the value of this pointer also gets overwritten, which, in turn, results in a crash, if a virtual function is called.
To avoid such mysterious crashes, memset should not be used on the objects of a class with virtual functions. Instead use the default constructor or an init routine to initialize member variables.
- 不要对含有virtual函数的类使用memset
- 千万不要对vector之类的容器使用memset
- 不要对动态分配的二维数组使用memset
- C++/C 千万别对结构体内部含有类使用memset赋值!!
- memset函数的使用
- memset函数的使用
- memset()的函数使用
- memset()函数的使用
- memset函数的使用
- memset函数的使用
- memset 函数 的使用
- memset函数的使用
- 对含有virtual function的class求sizeof
- 关于memset函数的使用
- memset函数的使用心得
- 关于memset() 函数的使用
- memset函数的使用陷阱
- memset函数的初步使用
- java另一种创建多线程的方式
- 三星大中国区总裁:创新1+1可能变成10
- 数据库“长连接”与“短连接”
- Qt5 Alpha Complie in XP with Mingw/vs2008
- 将图片的说明文字显示在图片之上且背景半透明效果
- 不要对含有virtual函数的类使用memset
- git 学习1:针对个人文档管理和协同开发
- 移动web app开发-----调试篇
- socket tcp 产生 rst响应的情况
- git 学习2:Git的项目开发模式 .
- git版本管理笔记整理
- java in think 多态问题
- exist-in和关联子查询-非关联子查询
- Android入门:TextView