使用memset而不是正确的ctor [英] Using memset instead of a proper ctor

查看:76
本文介绍了使用memset而不是正确的ctor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个Foo'的数组:


class Foo

{

public:

int SomeMethode(void);


int i;

char c;

char * p; < br $>
};


Foo FooArray [1000000];


是否可以初始化所有Foo'使用memset,

即memset(FooArray,0,sizeof(FooArray))?


如果没有,对案例最有效的安全初始化方法是什么

喜欢这个?


-Sacha

Suppose I have an array of Foo''s:

class Foo
{
public:
int SomeMethode(void);

int i;
char c;
char * p;
};

Foo FooArray[1000000];

Is it okay to initilaize all Foo''s by using memset,
i.e. memset( FooArray, 0, sizeof(FooArray) )?

If not, what is the most efficient safe initialization method for a case
like this?

-Sacha

推荐答案



" Sacha Sch?r" < SA ********** @ unibas.ch> skrev i en meddelelse

news:41 ********** @ news.bluewin.ch ...

"Sacha Sch?r" <sa**********@unibas.ch> skrev i en meddelelse
news:41**********@news.bluewin.ch...
假设我有一个Foo数组'的:

类Foo
{
公开:
int SomeMethode(void);

int i;
char c;
char * p;
};

Foo FooArray [1000000];

是否可以通过使用来初始化所有Foo'' memset,
即memset(FooArray,0,sizeof(FooArray))?


不。它可能适用于您的平台但不可移植。无论如何,如果memset

是正确的初始化方法,那么一个体面的优化编译器应该是
创建代码,这实际上是一个memset。

如果不是,这样一个案例最有效的安全初始化方法是什么?
编写一个执行正确操作的默认构造函数:

Foo :: Foo():i(0),c(0),p(0){}
-Sacha
Suppose I have an array of Foo''s:

class Foo
{
public:
int SomeMethode(void);

int i;
char c;
char * p;
};

Foo FooArray[1000000];

Is it okay to initilaize all Foo''s by using memset,
i.e. memset( FooArray, 0, sizeof(FooArray) )?
Nope. It might work on your platform but is not portable. Anyway, if memset
is the correct way of initialising, a decent optimising compiler should
create code which in effect is a memset.

If not, what is the most efficient safe initialization method for a case
like this? To write a default constructor that does the correct thing:

Foo::Foo(): i(0),c(0),p(0) {}
-Sacha



Sacha Sch?r写道:
Sacha Sch?r wrote:
假设我有一个Foo数组's:

class Foo
{
公开:
int SomeMethode(void);

int i;
char c;
char * p;
};

Foo FooArray [1000000];

是否可以通过初始化所有Foo'使用memset,
即memset(FooArray,0,sizeof(FooArray))?


问题不在于现在使用memset,当你改变关于Foo的东西时,问题可能会出现问题,比如为它添加一个虚拟函数。

在这种情况下建议的解决方案(见下文)将被标记为错误

而''memset''则不会。

如果不是,这样的案例最有效的安全初始化方法是什么?
Suppose I have an array of Foo''s:

class Foo
{
public:
int SomeMethode(void);

int i;
char c;
char * p;
};

Foo FooArray[1000000];

Is it okay to initilaize all Foo''s by using memset,
i.e. memset( FooArray, 0, sizeof(FooArray) )?
The problem is not with using memset now, the problem may present itself
when you change something about Foo, like add a virtual function to it.
Proposed solution in that case (see below) will be flagged as an error
while ''memset'' wouldn''t.

If not, what is the most efficient safe initialization method for a case
like this?




由于你的Foo似乎是一个POD对象,你可以使用大括号-enclosed zero-

初始化。


Foo FooArray [1000000000000] = {0};


免责声明: IIRC,0并不是必需的,即标准允许

只有空括号作为初始化器,但我遇到了一些

编译器,它们被{{{ }",但接受了至少一个零。


V



Since your Foo seems to be a POD object, you may use brace-enclosed zero-
initialiser.

Foo FooArray[1000000000000] = {0};

Disclaimer: IIRC, the 0 is not really necessary, i.e. the Standard allows
to have only the empty braces as the initialiser, but I encountered some
compilers that choked on "{}", but accepted at least one zero.

V


" Victor Bazarov" <五******** @ comAcast.net>在消息中写道

news:xv ******************* @ newsread1.mlpsca01.us.t o.verio.net ... < br>
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:xv*******************@newsread1.mlpsca01.us.t o.verio.net...
class Foo
{
公开:
int SomeMethode(void);

int i;
char c;
char * p;
};

Foo FooArray [1000000];

是否可以使用memset初始化所有Foo,
即memset(FooArray,0,sizeof(FooArray))?
class Foo
{
public:
int SomeMethode(void);

int i;
char c;
char * p;
};

Foo FooArray[1000000];

Is it okay to initilaize all Foo''s by using memset,
i.e. memset( FooArray, 0, sizeof(FooArray) )?



问题不在于现在使用memset,问题当你改变关于Foo的东西时,可能会出现这种情况,比如为它添加虚拟功能。



The problem is not with using memset now, the problem may present itself
when you change something about Foo, like add a virtual function to it.




此外,无法保证使用memset来设置一个指向

整数零的指针将产生一个有效的指针。



In addition, there is no guarantee that using memset to set a pointer to an
integer zero will yield a valid pointer.


这篇关于使用memset而不是正确的ctor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆