Using memset instead of a proper ctor

  • Thread starter =?ISO-8859-1?Q?Sacha_Sch=E4r?=
  • Start date
?

=?ISO-8859-1?Q?Sacha_Sch=E4r?=

Suppose I have an array of Foo's:

class Foo
{
public:
int SomeMethode(void);

int i;
char c;
char * p;
};

Foo FooArray[1000000];

Is it okay to initilaize all Foo's by using memset,
i.e. memset( FooArray, 0, sizeof(FooArray) )?

If not, what is the most efficient safe initialization method for a case
like this?

-Sacha
 
P

Peter Koch Larsen

Sacha Schär said:
Suppose I have an array of Foo's:

class Foo
{
public:
int SomeMethode(void);

int i;
char c;
char * p;
};

Foo FooArray[1000000];

Is it okay to initilaize all Foo's by using memset,
i.e. memset( FooArray, 0, sizeof(FooArray) )?

Nope. It might work on your platform but is not portable. Anyway, if memset
is the correct way of initialising, a decent optimising compiler should
create code which in effect is a memset.
If not, what is the most efficient safe initialization method for a case
like this?
To write a default constructor that does the correct thing:

Foo::Foo(): i(0),c(0),p(0) {}
 
V

Victor Bazarov

Sacha said:
Suppose I have an array of Foo's:

class Foo
{
public:
int SomeMethode(void);

int i;
char c;
char * p;
};

Foo FooArray[1000000];

Is it okay to initilaize all Foo's by using memset,
i.e. memset( FooArray, 0, sizeof(FooArray) )?

The problem is not with using memset now, the problem may present itself
when you change something about Foo, like add a virtual function to it.
Proposed solution in that case (see below) will be flagged as an error
while 'memset' wouldn't.
If not, what is the most efficient safe initialization method for a case
like this?

Since your Foo seems to be a POD object, you may use brace-enclosed zero-
initialiser.

Foo FooArray[1000000000000] = {0};

Disclaimer: IIRC, the 0 is not really necessary, i.e. the Standard allows
to have only the empty braces as the initialiser, but I encountered some
compilers that choked on "{}", but accepted at least one zero.

V
 
A

Andrew Koenig

class Foo
{
public:
int SomeMethode(void);

int i;
char c;
char * p;
};

Foo FooArray[1000000];

Is it okay to initilaize all Foo's by using memset,
i.e. memset( FooArray, 0, sizeof(FooArray) )?

The problem is not with using memset now, the problem may present itself
when you change something about Foo, like add a virtual function to it.

In addition, there is no guarantee that using memset to set a pointer to an
integer zero will yield a valid pointer.
 
?

=?ISO-8859-1?Q?Sacha_Sch=E4r?=

Since your Foo seems to be a POD object, you may use brace-enclosed zero-
initialiser.

Foo FooArray[1000000000000] = {0};

Yes, the Foo's are indeed POD's. I like this 'brace-enclosed
zero-initialiser', I never heard about it before. However, for the
actual case, FooArray is itself a member of a more complicated class.
Is it possible to use an an brace-enclosed initialiser also for members,
and if so, how?

-Sacha
 
V

Victor Bazarov

Sacha said:
Since your Foo seems to be a POD object, you may use brace-enclosed zero-
initialiser.

Foo FooArray[1000000000000] = {0};

Yes, the Foo's are indeed POD's. I like this 'brace-enclosed
zero-initialiser', I never heard about it before. However, for the
actual case, FooArray is itself a member of a more complicated class.
Is it possible to use an an brace-enclosed initialiser also for members,
and if so, how?

No, you're out of luck on this one. You need to give your Foo its own
default c-tor which will set (initialise) the members to 0.

V
 
?

=?ISO-8859-1?Q?Sacha_Sch=E4r?=

Andrew said:
class Foo
{
public:
int SomeMethode(void);

int i;
char c;
char * p;
};

Foo FooArray[1000000];

Is it okay to initilaize all Foo's by using memset,
i.e. memset( FooArray, 0, sizeof(FooArray) )?

The problem is not with using memset now, the problem may present itself
when you change something about Foo, like add a virtual function to it.


In addition, there is no guarantee that using memset to set a pointer to an
integer zero will yield a valid pointer.

So p = 0 and memset( &p, 0, sizeof(p) ) are not necessarily aquivalent
if p is a pointer?

-Sacha
 
R

Rolf Magnus

Sacha said:
Since your Foo seems to be a POD object, you may use brace-enclosed zero-
initialiser.

Foo FooArray[1000000000000] = {0};

Yes, the Foo's are indeed POD's. I like this 'brace-enclosed
zero-initialiser', I never heard about it before.

Actually, you can initialize with any value there. If you use less
initializers than you have elements, the other elements are default
initialized.
However, for the actual case, FooArray is itself a member of a more
complicated class. Is it possible to use an an brace-enclosed initialiser
also for members, and if so, how?

No. Unfortunately, C++ doesn't support initialization of array members.
 
D

Dietmar Kuehl

Victor said:
No, you're out of luck on this one. You need to give your Foo its own
default c-tor which will set (initialise) the members to 0.

That's incorrect: you can use a member initializer to default
initialize
a POD (or aggregate) class member:

/**/ struct foo {
/**/ foo():
/**/ array() // this has the same effect as 'Foo array[100000] =
{}'
/**/ {}
/**/ private:
/**/ Foo array[100000];
/**/ };

Although you need to use two different approaches in different
contexts,
tagging on parenthesis or brace initialization can always be used to
default initalize PODs or aggregates.
 
D

Dietmar Kuehl

Rolf said:
Unfortunately, C++ doesn't support initialization of array members.

It does not support initialization of array members with distinct
values but you can default initialize array members: simply tag a
pair of empty parenthesis to the member in the member initializer
list. See my other post in this thread for an example.
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top