memset 'this' in constructor?

H

hack_tick

hi Guys!

I am having a class, having around 300+ pointers ONLY, I need to set
them all NULL, when the object is created.

The best way I think is

memset(this, NULL, sizeof(classname));

whats your suggestion??
Will this break anything?? as it is done in constructor??

any suggestions???
 
H

hack_tick

Also guys what do you all suggest about deleting?? as this class is
only going to have Pointers as member variable,
 
K

kkk

hack_tick said:
hi Guys!

I am having a class, having around 300+ pointers ONLY, I need to set
them all NULL, when the object is created.

The best way I think is

memset(this, NULL, sizeof(classname));
This will turn the virtual fn pointer of your object to NULL.
 
P

Prawit Chaivong

hack_tick said:
hi Guys!

I am having a class, having around 300+ pointers ONLY, I need to set
them all NULL, when the object is created.

The best way I think is

memset(this, NULL, sizeof(classname));

whats your suggestion??
Will this break anything?? as it is done in constructor??

any suggestions???

You have at least two things to be concern.
1. Have you derived this class from anyclass?
If yes, but you might say that there isn't any data member in Base
class (please read 2)
2. Is there any virtual method inside this class?
 
N

Noah Roberts

hack_tick said:
hi Guys!

I am having a class, having around 300+ pointers ONLY, I need to set
them all NULL, when the object is created.

The best way I think is

memset(this, NULL, sizeof(classname));

whats your suggestion??
Will this break anything?? as it is done in constructor??

any suggestions???

Sounds like you got a blob on your hands. Factor it and make it more
maintainable. When you think, "I would like to memset because this
thing has too many damn variables," it is time to rethink your design.
 
H

hack_tick

hi
Prawit Chaivong wrote:
[....]
You have at least two things to be concern.
1. Have you derived this class from anyclass?
If yes, but you might say that there isn't any data member in Base
class (please read 2)
nope!!

2. Is there any virtual method inside this class?

nope

The class is not meant to be derived, it is just meant to handle all
those global variables into one namespace with heap control, so whats
your suggestion??
 
H

hack_tick

hi,

Noah Roberts wrote:
[...]
Sounds like you got a blob on your hands. Factor it and make it more
maintainable. When you think, "I would like to memset because this
thing has too many damn variables," it is time to rethink your design.

Actually I am porting existing app, to Symbian, which require me to
move all the global variables to Thread Local Storage (TLS), by making
them member to a class, as Symbian does not support Global Variables,
on some of the OS versions.
 
O

Old Wolf

hack_tick said:
I am having a class, having around 300+ pointers ONLY, I need to set
them all NULL, when the object is created.

The best way I think is

memset(this, NULL, sizeof(classname));

1) won't set pointers to NULL (will set them to all bits zero, which
might be different)

2) might roach other parts of your object (eg. a vtable).

If your question is "does this work in MSVC on windows XP"
then ask in some other newsgroup.

To initialize them properly then you should store all your pointers
in a container that will initialize all of its members, eg:

struct Foo
{
std::vector<T *> pointers;
Foo(): pointers(300, 0) {}
}

You could use a map<string, T *> if you want to refer to them by name.
 
A

Alf P. Steinbach

* hack_tick:
hi,

Noah Roberts wrote:
[...]
Sounds like you got a blob on your hands. Factor it and make it more
maintainable. When you think, "I would like to memset because this
thing has too many damn variables," it is time to rethink your design.

Actually I am porting existing app, to Symbian, which require me to
move all the global variables to Thread Local Storage (TLS), by making
them member to a class, as Symbian does not support Global Variables,
on some of the OS versions.

/One/ global variable is one too many.

And you have 300...

Anyways, default initialization (or per C++2003, value initialization)
will do the trick.

struct PODPointers
{
void* myGlobalPointer1;
void* myGlobalPointer2;
};

struct Pointers: PODPointers
{
Pointers(): PODPointers() {}
};
 
S

Steve Pope

hack_tick said:
The class is not meant to be derived, it is just meant to handle all
those global variables into one namespace with heap control, so whats
your suggestion??

What do you mean by "with heap control"?

If they are just a bunch of global variables, and you are
not creating multiple objects of this class, consider just
making them variables in a namespace and don't use a class.
Then they can be initialized statically, which cost nothing
in runtime.

Steve
 
N

Noah Roberts

hack_tick said:
hi,

Noah Roberts wrote:
[...]
Sounds like you got a blob on your hands. Factor it and make it more
maintainable. When you think, "I would like to memset because this
thing has too many damn variables," it is time to rethink your design.

Actually I am porting existing app, to Symbian, which require me to
move all the global variables to Thread Local Storage (TLS), by making
them member to a class, as Symbian does not support Global Variables,
on some of the OS versions.

Sounds like you got a real mess on your hands. Factor it and make it
more maintainable. When you see 300 global variables it is time to
rethink your design.
 
R

Rolf Magnus

Old said:
1) won't set pointers to NULL (will set them to all bits zero, which
might be different)

No. It will set each byte of each pointer to the value of the lowest byte of
NULL.
 
P

Phlip

hack_tick said:
I am having a class, having around 300+ pointers ONLY, I need to set
them all NULL, when the object is created.

The best way I think is

memset(this, NULL, sizeof(classname));

Firstly, memset(this, ...) is only well-behaved if your object is a PODS.
That means it only contains a Plain Old Data Structure. And as others have
pointed out, memset(pointers, NULL, ...) isn't typesafe.

Never rely on PODS unless you absolutely must, because even if you get it
right, you should someday want to add a feature to your class. If it breaks
POD-ness, nothing will warn you.

Now about the style...

Does each pointer have a different name, or are they all in an array?

Can you use a std::vector<pointer>? Then whatever loads the vector can NULL
each pointer as it goes in.

To free pointers returned from 'new', put a loop statement in your
destructor that calls delete on each pointer.
 
M

Michiel.Salters

Rolf said:
No. It will set each byte of each pointer to the value of the lowest byte of
NULL.

Since NULL isn't a pointer, but the null pointer constant, the lowest
byte of NULL
is 0, too. (C++ is not C)

HTH,
Michiel Salters
 
R

Rolf Magnus

Since NULL isn't a pointer, but the null pointer constant, the lowest
byte of NULL
is 0, too. (C++ is not C)

Doh! Of course. Well, at least, I showed one of the main reason to not use
NULL in the first place. ;-)
 
E

Earl Purple

Phlip said:
Can you use a std::vector<pointer>? Then whatever loads the vector can NULL
each pointer as it goes in.

I thnk that as he's using Symbian he can't use std::vector - there are
all sorts of issues with symbian and vector.

If one does have to use memset though, then at least put all of these
pointers into a nested POD struct and memset the struct. Then it won't
affect anything else you put in the class.
 
H

hack_tick

hi,
Earl said:
Phlip wrote: [...]

I thnk that as he's using Symbian he can't use std::vector - there are
all sorts of issues with symbian and vector.

I would say all sort of issues with STL and stdlib.
If one does have to use memset though, then at least put all of these
pointers into a nested POD struct and memset the struct. Then it won't
affect anything else you put in the class.

m...after having a long and really long thought, I have decided that
memset will not be a good idea in a long run, for now I have used a
very thin vector like class with some MACRO magic :-D (I know Macros
are bad, but will not be a very new thing for Symbian)

Thanks a lot guys!!!!
 

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,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top