关于非POD结构的memset [英] memset on structs with non-PODs
问题描述
亲爱的,
我现有一段带有一些POD结构的代码。
struct A
{
int x;
int y;
};
这个struct在某处创建并通过memset初始化。
A a;
memset(& a,0,sizeof(A));
现在我想用一个对象扩展结构,例如一个向量:
struct A
{
int x;
int y;
std :: vector< int> z;
};
序列
A a;
memset (& a,0,sizeof(A));
现在是致命的,因为我覆盖了实例化的矢量az
实际上结构是内置一堆POD非常庞大,所以使用
ctor是可能的,但是相当多的工作。 (我也害怕错过
的东西)。你会如何避免毁灭性的memset?
我想到了什么
struct A
{
int x;
int y;
int no_memsetbeyond_this_point;
std :: vector< int> z;
};
和
A a;
memset( & a,0,& a.no_memsetbeyond_this_point-& a);
这不是很好,但我必须避免尽可能多的代码返工
目前。
亲切问候,
Patrick
* Patrick Kowalzick:亲爱的,
我有一段带有一些POD的结构代码。
struct A
{
int x;
int y;
};
此结构在某处创建并通过memset初始化。
< mem a>
memset(& a,0,sizeof(A));
现在我想用一个对象扩展结构,例如vector:
struct A
{
int x;
int y;
std :: vector< int> z;
};
当你有公共非POD会员时,你需要重新设计。
序列
A a; memset(& a,0,sizeof(A));
现在会致命,因为我覆盖了实例化的矢量az
实际上结构里面有一堆POD是非常庞大的,所以使用
ctor是可能的,但是相当多的工作。 (我也害怕错过
的东西)。你会如何避免毁灭性的memset?
当你是一个非常巨大的时候结构你需要重新设计。
但是你可能需要在一些较小的测试程序上练习
。
现在,将原始结构重命名为PodA,
struct PodA {...};
然后从中派生结构A ,
结构A:PodA
{
A():PodA(){}
std :: vector< int> z;
};
并检查你的编译器是否支持默认初始化
POD'(不幸的是一些不要。
-
答:因为它弄乱了人们通常阅读文字的顺序。
问:为什么这么糟糕?
A:热门发布。
问:usenet和电子邮件中最烦人的是什么?
Hello Alf,
struct A
{
int x ;
int y;
std :: vector< int> z;
};
当你有公共的非POD成员时,你需要重新设计。
通常我永远不会使用struct更复杂的东西:)。
实际上结构非常庞大,内部有一堆POD,所以使用
a
> ctor是可能的,但是相当多的工作。 (我也害怕
想念
的东西)。你会如何避免毁灭性的memset?
当你是一个非常巨大的时候结构你需要重新设计。
嗯,可能是。让我们说在这种情况下非常巨大的东西不过是:我不想初始化所有成员。
现在,将原始结构重命名为PodA,
struct PodA {...};
然后从中派生出结构A.
结构A:PodA
{
A():PodA(){}
std :: vector< int> z;
};
完美。这是一个相当不错的解决方案。无论如何,对于这种情况,我将改为
struct PodA
{
PodA(){memset(this,0,sizeof (PODA)); }
...
};
struct A:PodA
{
std :: vector< int> z;
};
这不好(好吧很难看),但更接近原始代码。
并检查你的编译器是否支持默认初始化
POD'(不幸的是有些人不会)。
我不能,作为支持对于不同的编译器必须放心。
谢谢,这是非常好的解决方案。
问候,
Patrick
On Thu,2006年1月19日14:57:34 +0100,Patrick Kowalzick
< pa ** *************@mapandguide.de>写道:
亲爱的,
我有一段带有一些POD的结构代码。
struct A
{
int x;
int y;
};
此结构在某处创建并通过memset初始化。
> a;
memset(& a,0,sizeof(A));
现在我想用一个对象扩展结构,例如vector:
<结构A
{
int x;
int y;
std :: vector< int> z;
};
顺序
memset(& a,0,sizeof(A));
现在会致命,因为我会覆盖不变的矢量az
< snip>
你可以创建一个只包含POD数据的非虚基类,并从中继承
。在这种方式下,你仍然可以使用memset,但只能使用thiis
基类。
struct A_
{
int x;
int y;
};
struct A:public A_
{
A(){memset(static_cast< A_ *>(this),0,sizeof(A_);}
std :: vector< int> z;
};
问候,
Zara
Dear all,
I have an existing piece of code with a struct with some PODs.
struct A
{
int x;
int y;
};
This struct is created somewhere and initialized via a memset.
A a;
memset(&a,0,sizeof(A));
Now I want to extend the structure with an object, e.g a vector:
struct A
{
int x;
int y;
std::vector<int> z;
};
The sequence
A a;
memset(&a,0,sizeof(A));
would be fatal now, because I overwrite the instanciated vector a.z.
In reality the struct is very huge with a bunch of PODs inside, so using a
ctor would be possible, but quite a lot of work. (I am also scared to miss
something). How would you proceed to avoid the devastating memset?
I thought of kind of
struct A
{
int x;
int y;
int no_memsetbeyond_this_point;
std::vector<int> z;
};
and
A a;
memset(&a,0,&a.no_memsetbeyond_this_point-&a);
It is not really nice, but I have to avoid as much code-rework as possible
for the moment.
Kind regards,
Patrick
* Patrick Kowalzick:Dear all,
I have an existing piece of code with a struct with some PODs.
struct A
{
int x;
int y;
};
This struct is created somewhere and initialized via a memset.
A a;
memset(&a,0,sizeof(A));
Now I want to extend the structure with an object, e.g a vector:
struct A
{
int x;
int y;
std::vector<int> z;
};
When you have public non-POD members you need to redesign.
The sequence
A a;
memset(&a,0,sizeof(A));
would be fatal now, because I overwrite the instanciated vector a.z.
In reality the struct is very huge with a bunch of PODs inside, so using a
ctor would be possible, but quite a lot of work. (I am also scared to miss
something). How would you proceed to avoid the devastating memset?
When you a "very huge" struct you need to redesign.
But possibly you need to practice that on some smaller test programs
first.
For now, rename the orginal struct to PodA,
struct PodA { ... };
then derive struct A from that,
struct A: PodA
{
A(): PodA() {}
std::vector<int> z;
};
and also check that your compiler supports default-initialization of
POD''s (unfortunately some don''t).
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Hello Alf,
struct A
{
int x;
int y;
std::vector<int> z;
};
When you have public non-POD members you need to redesign.
Normally I''d never use struct for more complex things :).
In reality the struct is very huge with a bunch of PODs inside, so using
a
ctor would be possible, but quite a lot of work. (I am also scared to
miss
something). How would you proceed to avoid the devastating memset?
When you a "very huge" struct you need to redesign.
Hmm, might be. Lets say very huge in this context is nothing elses than: I
do not want to initialize all the members.
For now, rename the orginal struct to PodA,
struct PodA { ... };
then derive struct A from that,
struct A: PodA
{
A(): PodA() {}
std::vector<int> z;
};
Perfect. Thats a rather good solution. Anyway for this case I will change to
struct PodA
{
PodA() { memset(this,0,sizeof(PodA)); }
...
};
struct A: PodA
{
std::vector<int> z;
};
This is not nice (ok it is ugly), but closer to the original code.
and also check that your compiler supports default-initialization of
POD''s (unfortunately some don''t).
I can not, as support for different compiliers has to be assured.
Thanks, that''s really good solution.
Regards,
Patrick
On Thu, 19 Jan 2006 14:57:34 +0100, "Patrick Kowalzick"
<pa***************@mapandguide.de> wrote:
Dear all,
I have an existing piece of code with a struct with some PODs.
struct A
{
int x;
int y;
};
This struct is created somewhere and initialized via a memset.
A a;
memset(&a,0,sizeof(A));
Now I want to extend the structure with an object, e.g a vector:
struct A
{
int x;
int y;
std::vector<int> z;
};
The sequence
A a;
memset(&a,0,sizeof(A));
would be fatal now, because I overwrite the instanciated vector a.z.
<snip>
You may create a non-virtual base class with only POD data and inherit
from it. In such a way, you could still a memset, but only with thiis
base class.
struct A_
{
int x;
int y;
};
struct A:public A_
{
A() {memset(static_cast<A_ *>(this),0,sizeof(A_);}
std::vector<int> z;
};
Regards,
Zara
这篇关于关于非POD结构的memset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!