码迷,mamicode.com
首页 > 编程语言 > 详细

C++ 中memset 勿要对类使用

时间:2017-10-20 21:38:07      阅读:302      评论:0      收藏:0      [点我收藏+]

标签:unsigned   strong   rust   emc   target   等于   texture   material   超过   

C++ 中memset 勿要对类使用

 

参考链接: 
http://www.cppblog.com/qinqing1984/archive/2009/08/07/92479.html

百度百科第一次这么给力: 
void *memset(void *s, int ch, size_t n); 
函数解释:将s中前n个字节 (typedef unsigned int size_t )用 ch 替换并返回 s 。 
memset:作用是在一段内存块中填充某个给定的值,它是对较大的结构体或数组进行清零操作的一种最快方法。

memset() 函数常用于内存空间初始化: 

 char str[100];
 memset(str,0,100);

用来对一段内存空间全部设置为某个字符,一般用在对定义的字符串进行初始化为‘

memset(a, ‘\0‘, sizeof(a));

memcpy用来做内存拷贝,你可以拿它拷贝任何数据类型的对象,可以指定拷贝的数据长度:  

char a[100], b[50];
memcpy(b, a, sizeof(b)); //注意如用sizeof(a),会造成b的内存地址溢出。

strcpy就只能拷贝字符串了,它遇到’\0’就结束拷贝: 

char a[100], b[50];
strcpy(a,b);

如用strcpy(b,a),要注意a中的字符串长度(第一个‘\0’之前)是否超过50位,如超过,则会造成b的内存地址溢出。 
  

下面开始:

class Material
{
public:
    Material(){ setDefaults();}
    void setDefaults(){ memset(this,0,sizeof(*this));}
    int mark;
    char materialName[256]; // material name
    Vector3 ambient;    // ambient
    Vector3 diffuse;    // diffuse
    Vector3 specular;   // specular
    int shininess;  //
    float alpha;    //
    bool isSpecular;

char textureName[256];  // texture name
char textureTransName[256]; // transparent texture name

};

这段代码完美无瑕。再看看下面的:

class Material
{
public:
    Material(){ setDefaults();}
    void setDefaults(){ memset(this,0,sizeof(*this));}
    int mark;
    std::string materialName;   // material name
    Vector3 ambient;    // ambient
    Vector3 diffuse;    // diffuse
    Vector3 specular;   // specular
    int shininess;  //
    float alpha;    //
    bool isSpecular;

std::string textureName;    // texture name
std::string textureTransName;   // transparent texture name
};

上面的代码会造成内存泄露: 
所以对于C++的std::string来说,要使用C++风格的初始化。

在网上看到这样一条评论,觉得有道理: 
任何类都不能用memset, 一旦暴力,就等于你强奸了她的内部数据,她已经崩溃了

 

C++ 中memset 勿要对类使用

标签:unsigned   strong   rust   emc   target   等于   texture   material   超过   

原文地址:http://www.cnblogs.com/lsgxeva/p/7701361.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!