" memset的" vs" = {0}" ...如果您的初始化变量,它们是否相同? [英] "memset" vs "= {0}"...Are they equivalent if your initializing variables?

查看:72
本文介绍了" memset的" vs" = {0}" ...如果您的初始化变量,它们是否相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有一个结构:


struct MYSTRUCT

{

int x;

int y;

int w;

int h;

};


和你想要声明该结构的实例并将其初始化

为零。是memset吗?必要或者我可以简单地将它设置为等于

到{0}。


这是:

MYSTRUCT myStruct = {0 };


与此相同:

MYSTRUCT myStruct;

memset(& myStruct,0,sizeof(MYSTRUCT) );





第一种方式似乎更快,但我看到很多人这样做了

第二种方式。有人可以就这个问题说清楚吗?

TIA。

解决方案



<否**** @ runtime.com>在消息中写道

新闻:fq ******************************** @ 4ax.com ...

假设你有一个结构:

struct MYSTRUCT
{
int x;
int y;
int w;
int h;
};

并且您想要声明该结构的实例并将其初始化为零。是memset吗?必要或者我可以简单地将它设置为等于{0}。

这是:
MYSTRUCT myStruct = {0};

相同如下:
MYSTRUCT myStruct;
memset(& myStruct,0,sizeof(MYSTRUCT));



第一种方式似乎更快,


为什么你说它似乎更快?判断它是否更快的唯一方法是

时间(或者如果你非常了解生成的机器

代码)。 Remeber编译器通常是优化代码的漂亮代码,我想

当然希望两个方法在一个好的
编译器上完全相同(但我还没有实际检查过这个。)

但我看到很多人这样做的第二种方式。有人可以就这个问题说清楚吗?


对于您声明的结构,这两种方式是相同的。但是如果你有一个

结构,其成员有一个构造函数(例如一个std :: string),那么memset

就错了。所以这就是第二种方法的优点,它在

更多情况下工作,你可以合理地希望得到一个编译器错误

消息,如果你错误地使用它。 />
TIA。




john


2004年9月22日星期三03:17: 47 -0500, No****@runtime.com 写道:

说你有一个结构:

struct MYSTRUCT
{
int x;
int y;
int w;
int h;
这是:
MYSTRUCT myStruct = {0};

相同如下:
MYSTRUCT myStruct;
memset(& myStruct,0,sizeof(MYSTRUCT));



第一种方式似乎更快,但我看到很多人这样做的第二种方式。有人可以就这个问题说清楚吗?




第一种方式通常更好,因为零初始化所有

成员。 Memsetting为0并不总是与零初始化相同,

特别是对于浮点和指针成员,它们可能有0

值,这些值都不是零。


性能明智,我不知道哪个会更快,你必须用b $ b来衡量它。

Tom


>会错的。这就是第二种方法的优点,它适用于


我的意思是第一种方法。


john

Say you have a struct:

struct MYSTRUCT
{
int x;
int y;
int w;
int h;
};

and you want to declare an instance of that struct and initialize it
to zero. Is "memset" necessary or can i simply initially set it equal
to {0}.

Is this:
MYSTRUCT myStruct = {0};

the same as this:
MYSTRUCT myStruct;
memset(&myStruct, 0, sizeof(MYSTRUCT));

?

The first way seems faster, but I see alot of people doing it the
second way. Could someone please shed some light on the subject?
TIA.

解决方案


<No****@runtime.com> wrote in message
news:fq********************************@4ax.com...

Say you have a struct:

struct MYSTRUCT
{
int x;
int y;
int w;
int h;
};

and you want to declare an instance of that struct and initialize it
to zero. Is "memset" necessary or can i simply initially set it equal
to {0}.

Is this:
MYSTRUCT myStruct = {0};

the same as this:
MYSTRUCT myStruct;
memset(&myStruct, 0, sizeof(MYSTRUCT));

?

The first way seems faster,
Why do you say it ''seems faster''? The only way to tell if it is faster is to
time it (or if you are very knowledgable look at the generated machine
code). Remeber compilers are usually pretty code at optimising code, I would
certainly hope that the two methods would be exactly the same on a good
compiler (but I haven''t actually checked this).
but I see alot of people doing it the
second way. Could someone please shed some light on the subject?
The two ways are the same for the struct you have declared. But if you had a
struct with members that had a constructor (e.g. a std::string) then memset
would be wrong. So that is the advantage of the second method, it works in
more situations and you could reasonable hope to get a compiler error
message if you used it incorrectly.
TIA.



john


On Wed, 22 Sep 2004 03:17:47 -0500, No****@runtime.com wrote:

Say you have a struct:

struct MYSTRUCT
{
int x;
int y;
int w;
int h;
};

and you want to declare an instance of that struct and initialize it
to zero. Is "memset" necessary or can i simply initially set it equal
to {0}.

Is this:
MYSTRUCT myStruct = {0};

the same as this:
MYSTRUCT myStruct;
memset(&myStruct, 0, sizeof(MYSTRUCT));

?

The first way seems faster, but I see alot of people doing it the
second way. Could someone please shed some light on the subject?



The first way is generally better, since it zero initializes all
members. Memsetting to 0 is not always the same as zero initializing,
particularly for floating point and pointer members, which may have 0
values that aren''t all bits zero.

Performance wise, I don''t know which would be faster, you''d have to
measure it.

Tom


> would be wrong. So that is the advantage of the second method, it works in

I meant first method.

john


这篇关于&QUOT; memset的&QUOT; vs&quot; = {0}&quot; ...如果您的初始化变量,它们是否相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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