P
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;
};
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
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