473,382 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

clearing all members of a class

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?

--
Elias
Jul 22 '05 #1
9 1678
lallous wrote:
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.

--
Karthik.
Jul 22 '05 #2
lallous wrote:
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;
}
};


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #3
Ioannis Vranos wrote:
lallous wrote:
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;
}
};


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

Jul 22 '05 #4
Thomas Matthews wrote:
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. :-)


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #5
Thomas Matthews wrote:
Or using initialization lists:
class X
{
int x;
int y;
X()
: x(0), y(0)
{ ; }
};

';' is not needed inside the X() body. :-)


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #6
"Karthik Kumar" <ka*******************@yahoo.com> wrote in message
news:41e39488$1@darkstar...
lallous wrote:
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.


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

--
Elias
Jul 22 '05 #7
"Ioannis Vranos" <iv*@remove.this.grad.com> wrote in message
news:1105440058.5756@athnrd02...
lallous wrote:
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;
}
};

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.

--
Elias
Jul 22 '05 #8
lallous wrote:
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.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #9
lallous wrote:


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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Carlos Ribeiro | last post by:
I thought about this problem over the weekend, after long hours of hacking some metaclasses to allow me to express some real case data structures as Python classes. I think that this is something...
18
by: Niels | last post by:
Hi group, I have some problems with clearing floated divs. My usual method works fine in Konqueror, but not in Firefox. Here's an example: <html><body> <div id='left' style='float:left;...
8
by: CoolPint | last post by:
I read in books that nested class cannot access private members of nesting class and vice versa unless they are made friends. Somehow, my compiler is letting my nested class member functions access...
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
10
by: Liz - Newbie | last post by:
Does anyone know how to clear arrays? My C# books talk about creating arrays or talk about using Clear or RemoveAt but these methods don't appear to be available for my array. I have an array...
15
by: roberts.noah | last post by:
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...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
13
by: yb | last post by:
Hi, Is there a CSS method to clear a float such that it aligns with the left content edge. For example: X X X X X X X X
3
by: Martin | last post by:
Is clearing a structure the following way well defined in C89? The structure ACTION contains no floating point or pointer members. Only integral types. My thoughts concern the padding - can and...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.