使用成员函数清除结构。 [英] Clearing structures with member functions.

查看:76
本文介绍了使用成员函数清除结构。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些代码,这些代码在

中调用了memset(this,0,sizeof(* this)),这是C ++中结构的成员函数。这只是看起来不错,所以我做了一些网络搜索,确实看起来像是一个可疑的事情。环顾四周还带我去了一个

新闻组的讨论,他说更好的方法就是做一些事情

就像sv = Struct()一样。问题是这两种情况似乎都很好。所以

只要没有虚函数而且Struct中没有构造函数或

任何内部函数。像这样的代码不会将对象初始化为

0:

struct SOut {

int x;

struct SIn {

int y;

SIn:y(){} //或者这可能是一个虚函数...

} in;

}


....


SOut out = SOut(); //初始化y而不是x。


out = SOut(); //清除y而不是x ...


因此,如果在某些情况下使用任何一种方法中断

哪个更正确的问题似乎不清楚。由于这是一个

性能问题,真正的问题是哪个更快,memset(x,

0,sizeof(x))还是x = X()?另一个问题是,如果上面的代码应该清除x的值...只要SIn没有构造函数或

虚函数它可以工作,但是只要它确实是SOut的默认

构造函数不再清除x。 C ++中的哪些法律管理这个

的行为?


使用memset不道德即使你知道对象永远不会是
任何美德?我认为可能的唯一原因是

这些结构具有成员函数...并且在此调用memset

/似乎/ icky。我可以肯定地看到一个关于这个的争论而且我想要确定我的位置......和IANAL。


谢谢。

解决方案

ro *** *******@gmail.com 写道:

struct SOut {
int x;
struct SIn {
int y;
SIn:y(){} //或者这可能是一个虚函数......
} in;
}




为什么不立即为SOut生成一个构造函数并且一起避免memset所有




-Josh McFarlane




< ro ********** @ gmail.com>在消息中写道

news:11 ********************** @ g14g2000cwa.googlegr oups.com ...

我在一个结构的成员函数中运行了一些调用memset(this,0,sizeof(* this))的代码。这对我来说只是看错了,所以我做了一些网络搜索,看起来确实有点可疑。环顾四周进一步让我参加了一个新闻组的讨论,他说更好的方法就是做一些像sv = Struct()这样的事情。问题是这两种情况似乎都很好。所以
只要没有虚函数,没有构造函数在Struct或
任何内部。像这样的代码不会将对象初始化为
0:


并且没有指针或浮点成员。全部位零是

不能保证评估为NULL或0.0

结构SOut {
int x;
struct SIn {
int y;
SIn:y(){} //或者这可能是一个虚函数...
} in;
}

...

SOut out = SOut(); //初始化y而不是x。

out = SOut(); //清除y而不是x ...

因此,如果在某些情况下使用任何一种方法都会中断
哪个更正确的问题似乎不清楚。由于这是一个性能问题



你确定吗?您是否通过有效测量证明了这一点?

真正的问题是哪个更快,memset(x,
0,sizeof(x))还是x = X()?


其中一个。的任何一方。他们都不是。语言

对性能没有任何说明(除了标准库

''big-O''规格)。

另一个问题是如果上面的代码应该清除x的值......只要SIn没有构造函数或者它可以工作的虚函数,但是只要它执行SOut的默认构造函数no更长时间清除x。 C ++中的哪些法律管理这种行为?


我会让别人看一下,同时建议

反对在这种情况下使用memset。
这些结构具有成员函数......并且在这个上调用memset
/似乎/ icky。


IMO不仅仅是''icky'',而是一种危险的做法。

我可以肯定地看到一个关于这个的争论而且我会
喜欢确定我的位置...和IANAL。

谢谢。




我认为更好的问题是为什么需要在一次扫描中清零所有

a类'的数据成员? IMO每个数据成员都有一个特定的意义和目的,以及操作它的功能。如果在

处理过程中,一个或多个数据成员需要重置,

这应该在该逻辑中明确地完成,例如


{

/ * etc * /

xa = 0;

xb = 0;

/ * etc * /

}


许多程序员认为他们可以手动优化代码比

他们的编译器可以。他们几乎总是错的。


-Mike




Mike Wahler写道:< blockquote class =post_quotes>< ro ********** @ gmail.com>在消息中写道
新闻:11 ********************** @ g14g2000cwa.googlegr oups.com ...

我在C ++中的结构的成员函数中遇到了一些调用memset(this,0,sizeof(* this))的代码。这对我来说只是看错了,所以我做了一些网络搜索,看起来确实有点可疑。环顾四周进一步让我参加了一个新闻组的讨论,他说更好的方法就是做一些像sv = Struct()这样的事情。问题是这两种情况似乎都很好。所以
只要没有虚函数,没有构造函数在Struct或
任何内部。像这样的代码不会将对象初始化为0:
并且没有指针或浮点成员。所有位零都不能保证评估为NULL或0.0




我们不关心某些模糊,不存在的行为/>
实现其中NULL不为0.实际上我从未见过

这样的事情发生了。由于编译器,硬件,操作系统以及所有已知_I_的所有位都可以保证所有位零为空且为0.0。


如果我要说服其他人,我们需要改变这种做法,我不能说这些论点是我们唯一的观点,对于我们和我们的目标他们是

moot。这里的性能是否符合标准的要求

所以,除非有一些*实际*原因,这种做法不会改变。

如果那里有一块硬件使用其他东西

比0意味着0.0或null然后MAYBE,但afaik没有这样的东西存在

和运行Windows的x86肯定不是其中之一。 />

我同意取决于一个实现是不好的形式,但我是在Windows家里工作的
,这很明显是这样的规范

情况。我的操作系统,硬件和编译器不太可能以这样的方式改变,而且没有人关心与某些东西的兼容性

模糊或理论。

真正的问题是哪个更快,memset(x,
0,sizeof(x))或x = X()?



其中一个。的任何一方。他们都不是。语言
没有说明性能(除了标准库
''大O''规格)。




我希望一个实际的答案。

我认为一个更好的问题是为什么需要在一次扫描中清零所有类数据成员?




请注意,我确实指定了结构而不是类。


I ran across some code that called memset(this, 0, sizeof(*this)) in
the member function of a structure in C++. This just looked wrong to
me so I did some web searching and it does indeed seem like a
questionable thing to do. Looking around further took me to a
newsgroup discussion that said the better approach was to do something
like sv = Struct(). The thing is that both cases seem to bo "ok" so
long as there are no virtual functions and no constructor in Struct or
any of its internals. Code like this does not initialize the object to
0:

struct SOut {
int x;
struct SIn {
int y;
SIn : y() {} // or this could be a virtual function...
} in;
}

....

SOut out = SOut(); // initializes y but not x.

out = SOut(); // clears y but not x...

So, since using either approach breaks if given certain circumstances
the question of which is more correct seems unclear. Since this is a
performance issue the real question is which is the faster, memset(x,
0, sizeof(x)) or x = X()? Another question is if the above code should
be clearing the value of x...so long as SIn has no constructor or
virtual functions it works, but as soon as it does the default
constructor of SOut no longer clears x. What law in C++ governs this
behavior?

Is using memset immoral even if you KNOW that the object will never
have any virts? The only reason why I would think it might be is that
these structs have member functions...and calling memset on this
/seems/ icky. I can definately see an argument comming over this and I
would like to be sure of my position...and IANAL.

Thanks.

解决方案

ro**********@gmail.com wrote:

struct SOut {
int x;
struct SIn {
int y;
SIn : y() {} // or this could be a virtual function...
} in;
}



Why not just generate a constructor for SOut then and avoid memset all
together?

-Josh McFarlane



<ro**********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...

I ran across some code that called memset(this, 0, sizeof(*this)) in
the member function of a structure in C++. This just looked wrong to
me so I did some web searching and it does indeed seem like a
questionable thing to do. Looking around further took me to a
newsgroup discussion that said the better approach was to do something
like sv = Struct(). The thing is that both cases seem to bo "ok" so
long as there are no virtual functions and no constructor in Struct or
any of its internals. Code like this does not initialize the object to
0:
And no pointer or floating point members. All-bits-zero is
not guaranteed to evaluate to NULL or 0.0

struct SOut {
int x;
struct SIn {
int y;
SIn : y() {} // or this could be a virtual function...
} in;
}

...

SOut out = SOut(); // initializes y but not x.

out = SOut(); // clears y but not x...

So, since using either approach breaks if given certain circumstances
the question of which is more correct seems unclear. Since this is a
performance issue

Are you sure? Have you proven so with valid measurements?
the real question is which is the faster, memset(x,
0, sizeof(x)) or x = X()?
One of them. Either of them. Neither of them. THe language
says nothing about performance (other than the standard library
''big-O'' specs).
Another question is if the above code should
be clearing the value of x...so long as SIn has no constructor or
virtual functions it works, but as soon as it does the default
constructor of SOut no longer clears x. What law in C++ governs this
behavior?
I''ll let someone else look that up, and at the same time advise
against using memset in that context.

Is using memset immoral even if you KNOW that the object will never
have any virts? The only reason why I would think it might be is that
these structs have member functions...and calling memset on this
/seems/ icky.
IMO not just ''icky'', but a dangerous practice.
I can definately see an argument comming over this and I
would like to be sure of my position...and IANAL.

Thanks.



I think a better question is why is there a need to ''zero-out'' all
a class'' data members in one sweep? IMO each data member has a specific
meaning and purpose, along with function(s) to manipulate it. If in the
course of processing, one or more data members need to be ''reset'',
this should be done in that logic, unambiguously, e.g.

{
/* etc */
x.a = 0;
x.b = 0;
/* etc */
}

Many programmers think they can optimize code by hand better than
their compiler can. They''re almost always wrong.

-Mike



Mike Wahler wrote:

<ro**********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...

I ran across some code that called memset(this, 0, sizeof(*this)) in
the member function of a structure in C++. This just looked wrong to
me so I did some web searching and it does indeed seem like a
questionable thing to do. Looking around further took me to a
newsgroup discussion that said the better approach was to do something
like sv = Struct(). The thing is that both cases seem to bo "ok" so
long as there are no virtual functions and no constructor in Struct or
any of its internals. Code like this does not initialize the object to
0:
And no pointer or floating point members. All-bits-zero is
not guaranteed to evaluate to NULL or 0.0



We are not concerned with the behavior of some obscure, non-existant
implementation in which NULL is not 0. In practice I have never seen
such a thing occur. As the compiler, hardware, OS, and all that is
known _I_ can guarantee that all bits zero is null and 0.0.

If I am to convince others that we need to change this practice I can''t
make such arguments my sole point as for us and our targets they are
moot. Performance is given precidence over standards compliance here
so unless there is some *practical* reason the practice won''t change.
If there is a piece of hardware out there that uses something other
than 0 to mean 0.0 or null then MAYBE, but afaik no such thing exists
and the x86 running Windows is certainly not one of them.

I agree that depending on an implementation is bad form, but I am
working in a Windows house and that is just plain the norm under such
situations. My OS, hardware, and compiler are not likely to change in
such a manner and nobody is concerned with compatibility with something
obscure or theoretical.

the real question is which is the faster, memset(x,
0, sizeof(x)) or x = X()?



One of them. Either of them. Neither of them. THe language
says nothing about performance (other than the standard library
''big-O'' specs).



I was hoping for a practical answer.
I think a better question is why is there a need to ''zero-out'' all
a class'' data members in one sweep?



Note that I did specify structure and not class.


这篇关于使用成员函数清除结构。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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