clearing all members of a class

L

lallous

Hello,

Given:

class X
{
int x, y; // and many many more....
X()
{
memset((void*)this, 0, sizeof(X));
}
};

Is this a good way to clear *all* members of a class?
 
K

Karthik Kumar

lallous said:
Hello,

Given:

class X
{
int x, y; // and many many more....
X()
{
memset((void*)this, 0, sizeof(X));
}
};

Is this a good way to clear *all* members of a class?

This is definitely inviting trouble.

You are making the assumption that -
sizeof (X) = sizeof(x) + sizeof(y) ( sum of individual
variables ), which is not correct .

What if the class got a virtual members ,
and hence a VTBL (virtual table) is constructed.
You are going to overwrite that too .
Let's see this example here.

#include <iostream>

using std::cout;
using std::endl;

class A {
public:
virtual void doNothing() = 0;

void erase() {
memset((void*)this, 0, sizeof(A));
}
private :
int a;
};

class B : public A {

public:
void doNothing() { }
};

int main() {
A * p = new B;
cout << sizeof(A) << " " << sizeof(int) << endl;
p->erase(); //Oops - You are erasing the VTBL here.
//How will I know my type from now on ?

cout << sizeof(A) << " " << sizeof(int) << endl;
p->doNothing(); //Segmentation fault ....
delete p;
}


And this is extremely non-intuitive and buggy.
That explains why you should never use this.
 
I

Ioannis Vranos

lallous said:
Hello,

Given:

class X
{
int x, y; // and many many more....
X()
{
memset((void*)this, 0, sizeof(X));
}
};

Is this a good way to clear *all* members of a class?


No. You can do this:

class X
{
int x, y; // and many many more....
X()
{
x=y=0;
}
};
 
T

Thomas Matthews

Ioannis said:
No. You can do this:

class X
{
int x, y; // and many many more....
X()
{
x=y=0;
}
};

Or using initialization lists:
class X
{
int x;
int y;
X()
: x(0), y(0)
{ ; }
};



--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
I

Ioannis Vranos

Thomas said:
Or using initialization lists:
class X
{
int x;
int y;
X()
: x(0), y(0)
{ ; }
};


Yes, I used that notion because of the comment // and many many more....
(variables). In which case he should use vectors or something, but
anyway. :)
 
L

lallous

Karthik Kumar said:
This is definitely inviting trouble.

You are making the assumption that -
sizeof (X) = sizeof(x) + sizeof(y) ( sum of individual
variables ), which is not correct .

What if the class got a virtual members ,
and hence a VTBL (virtual table) is constructed.
You are going to overwrite that too .
Let's see this example here.

#include <iostream>

using std::cout;
using std::endl;

class A {
public:
virtual void doNothing() = 0;

void erase() {
memset((void*)this, 0, sizeof(A));
}
private :
int a;
};

class B : public A {

public:
void doNothing() { }
};

int main() {
A * p = new B;
cout << sizeof(A) << " " << sizeof(int) << endl;
p->erase(); //Oops - You are erasing the VTBL here.
//How will I know my type from now on ?

cout << sizeof(A) << " " << sizeof(int) << endl;
p->doNothing(); //Segmentation fault ....
delete p;
}


And this is extremely non-intuitive and buggy.
That explains why you should never use this.

What if this was a simple struct that is not to be extended /
subclasses...is memset() healthy?
 
L

lallous

Ioannis Vranos said:
No. You can do this:

class X
{
int x, y; // and many many more....
X()
{
x=y=0;
}
};
Hello

I am aware of this method, however what if I got lots of member
variables...should I be initializing them individually one by one?
I used memset() to clear all the data members.
 
I

Ioannis Vranos

lallous said:
Hello

I am aware of this method, however what if I got lots of member
variables...should I be initializing them individually one by one?
Yes.

I used memset() to clear all the data members.


You shouldn't. For instance for floating point variables this method is
not safe. If you have the need to use many variables of the same built
in type, then perhaps you should use a vector of them, which also
initialises them to 0.
 
K

Karl Heinz Buchegger

lallous said:
I am aware of this method, however what if I got lots of member
variables...should I be initializing them individually one by one?
I used memset() to clear all the data members.

Don't.
memset(), memcpy(), ... have their usage even in C++
But those functions are far to dangerous and don't fit very
well in an Object oriented world. Their usage should be
restricted to situations where you deal with bytes on a
very hardware-near level only.

Everything else is asking for troubles in the long run.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,185
Latest member
GluceaReviews

Latest Threads

Top