Click to See Complete Forum and Search --> : ZeroMemory and pointer size


hspc
July 25th, 2003, 03:26 AM
Hi.. I have a class :

CItem
{
int num;
CItem* pItem;
}


I want to know the size of the class..
So i calculate it this way : int = 4 byte , Pointer=4 byte
then the class consumes 8 bytes

is this true ?

also i want to reset num and pItem using ZeroMemory in the constructor .. can I say :

ZeroMemory(this,8)

rxbagain
July 25th, 2003, 03:40 AM
Yes, the class contains 8 bytes. it is better, however, to use the sizeof(CItem) than to hardcode the size for maintenance and portability issue.

Hope it will help you

Paul McKenzie
July 25th, 2003, 10:54 PM
Originally posted by hspc
Hi.. I have a class :

CItem
{
int num;
CItem* pItem;
}


I want to know the size of the class..
So i calculate it this way : int = 4 byte , Pointer=4 byte
then the class consumes 8 bytes

is this true ?
Maybe, maybe not. Use sizeof() to get the size of the class.also i want to reset num and pItem using ZeroMemory in the constructor .. can I say :

ZeroMemory(this,8)
First, you don't know that the size of the stuct is 8. Never assume or hard-code what the size of the data type is -- always use sizeof().

Second, what do you mean by "reset" in the constructor? The object is being created for the first time, so how can you "reset" something that wasn't set?

Third, you used this, but your class has no member functions (which is the only place you can specify this)

Fourth, if your class will have member functions, you can't use ZeroMemory. ZeroMemory is meant for POD (Plain-Old-Data) types, structures, and classes i.e. ints, doubles, floats, chars, pointers, arrays of these types, and structures containing only these types. ZeroMemory cannot be used for classes or structs that contain member functions or member variables that have constructors. Why not just simply set num to 0 and pItem to NULL in the member initialisation list of the default constructor?

class CItem
{
public:
CItem() : num(0), pItem(NULL) { }

private:
int num;
CItem *pItem;
};

Regards,

Paul McKenzie

hspc
July 26th, 2003, 07:10 AM
thank you for replies ..
I want to clear a point :
I ment by (reset) to make this :
in constructor :

num=0;
pItem=NULL;

so I would better have used (Initialize)

also .. I want to know how (sozeof) works..
does it get the size in runtime ?

if i can't use ZeroMemory (coz my class has functions) is there another way ?(i don't want to zero all poiners one by one)
thank you..

Paul McKenzie
July 26th, 2003, 07:36 AM
Originally posted by hspc
(i don't want to zero all poiners one by one)
thank you.. Why not? How many pointers are there? If there are so many pointers that it is so hard to make each of them NULL, then you have a class design problem.

First, do you need pointers? What exactly are you trying to accomplish with the class you are writing? It looks like a linked list of some type. If this is the case, you have std::list<> and you don't need to code anything.

Second, if you do need pointers, and if the pointers are the same, why not make an array of these pointers and just loop to clear each one out.

Third option: why not store the pointer data in a separate structure and call ZeroMemory on that structure. In your class, just have an instance of this structure (or you can have the structure as part of the class).

sizeof() returns the size in bytes the data type. Since sizeof() is elementary C and C++, your C++ book can discuss this better than I can.

Regards,

Paul McKenzie

hspc
July 26th, 2003, 02:36 PM
thank you Paul McKenzie
It's not a linked list .. It's something like a B-Tree..
the Idea of the structue looks great ... :)