Memory initialization

M

mcassiani

Hi,

I need to zero init a class with many variables (members),
what is the best method to do it withoun init separately each members.

es:

class test{
int a1;
..
...
int a100
char v1[100]
...
ichar v100[100]
}

Marco
 
J

JustSomeGuy

mcassiani said:
Hi,

I need to zero init a class with many variables (members),
what is the best method to do it withoun init separately each members.

es:

class test{
int a1;
..
..
int a100
char v1[100]
...
ichar v100[100]
}

Marco

Although this might be dangerous you might try using memset or bzero
functions using the sizeof(class test) as a paramters...
I belive this is not a very 'object' oriented approach...

a1 = a100 = v1 = v100 = 0; // should work as well...
 
M

Martin M. Pedersen

JustSomeGuy said:
Although this might be dangerous you might try using memset or bzero
functions using the sizeof(class test) as a paramters...
I belive this is not a very 'object' oriented approach...

If the class is a POD structure, as it seems to be in this case, memset()
would be my solution too. On the other hand, if it is really a class -
perhaps with a vtable - copying a static instance with zero values works
better (read: memset() does not work if there is a vtable).

Regards,
Martin M. Pedersen
 
I

Ioannis Vranos

mcassiani said:
Hi,

I need to zero init a class with many variables (members),
what is the best method to do it withoun init separately each members.

es:

class test{
int a1;
..
...
int a100
char v1[100]
...
ichar v100[100]
}

Marco


If members are public:


class test
{
public:

int a1, a2;

char v1[100], v2[100];
};


int main()
{
test whatever={0,0, {0}, {0}};
}

The same apply for a struct.



For private members you will have to use a constructor.






Best regards,

Ioannis Vranos
 
P

Peter Koch Larsen

Martin M. Pedersen said:
If the class is a POD structure, as it seems to be in this case, memset()
would be my solution too. On the other hand, if it is really a class -
perhaps with a vtable - copying a static instance with zero values works
better (read: memset() does not work if there is a vtable).

memset just does not work for floating point types. Thus my recommendation
is to use standard constructors and expect the optimizer to be intelligent
enough to do a memset if that is the proper thing to do.

/Peter
 
J

JustSomeGuy

Peter said:
memset just does not work for floating point types. Thus my recommendation
is to use standard constructors and expect the optimizer to be intelligent
enough to do a memset if that is the proper thing to do.

VERY TRUE.
 
D

Dave Townsend

If you need to do something like this, you might be doing things the
hard way!
 
D

David Rubin

Ioannis Vranos said:
mcassiani said:
Hi,

I need to zero init a class with many variables (members),
what is the best method to do it withoun init separately each members.

es:

class test{
int a1;
..
...
int a100
char v1[100]
...
ichar v100[100]
}

Marco


If members are public:


class test
{
public:

int a1, a2;

char v1[100], v2[100];
};


int main()
{
test whatever={0,0, {0}, {0}};
}

test t = {0};

will suffice. If you need to re-initialize a test instance to all
zeros, something like

test z = {0};
test t;

// ...

t = z;

works well. As you point out, if test has private members, or non-POD
members, you need to implement various constructors. If you have many
private data elements in a class, it may be worthwhile to partially
insulate the class as

// test.h
class test_i; // private members, defined in test.cpp
class test {
test_i *d_this;
public:
test(); // non-inline definition allocates test_i
~test() { delete d_this; }
// rest of public interface
};

Then, inside test.cpp, you can use the above technique to initialize
*d_this to zeros.

/david
 
S

Siemel Naran

Peter Koch Larsen said:
memset just does not work for floating point types. Thus my recommendation
is to use standard constructors and expect the optimizer to be intelligent
enough to do a memset if that is the proper thing to do.

Just curious. If all the bits of a double are zero, then what is the value
of the double? It's not necessarily zero? What do some implementations do?
 
S

Siemel Naran

mcassiani said:
I need to zero init a class with many variables (members),
what is the best method to do it withoun init separately each members.

es:

class test{
int a1;
..
..
int a100
char v1[100]
...
ichar v100[100]
}

Fundamental types at global or class static scopes are zero initialized. An
array or struct consisting of fundamental types or other such structs also
qualifies. Thus I think the following should work. It works on my
compiler, and maybe will work on all:

class test{
public:
test() { *this = zero; }
int a;
double b;
private:
class NoInitialize { };
test(NoInitialize) { }
static test zero;
};

test test::zero;
 
I

Ioannis Vranos

JustSomeGuy said:
Although this might be dangerous you might try using memset


memset() can only be used to unsigned chars since any other type may
contain padding bits.





What is bzero?


functions using the sizeof(class test) as a paramters...
I belive this is not a very 'object' oriented approach...

a1 = a100 = v1 = v100 = 0; // should work as well...


With v1, v100 being arrays this should not work.






Best regards,

Ioannis Vranos
 
I

Ioannis Vranos

Siemel said:
Just curious. If all the bits of a double are zero, then what is the value
of the double? It's not necessarily zero? What do some implementations do?



It is implementation dependent.






Best regards,

Ioannis Vranos
 
S

Siemel Naran

Ioannis Vranos said:
memset() can only be used to unsigned chars since any other type may
contain padding bits.

So what? You can set the padding bits to zero too!
 
I

Ioannis Vranos

To make my statement above more precise:

"memset() can only be used to unsigned chars since any other type may
contain (bytes with) padding bits."



So what? You can set the padding bits to zero too!



And then we get undefined behaviour.






Best regards,

Ioannis Vranos
 
I

Ioannis Vranos

Ioannis said:
To make my statement above more precise:

"memset() can only be used to unsigned chars


and chars, signed chars (since stand alone signed chars can have the
value 0)

since any other type may
contain (bytes with) padding bits."








And then we get undefined behaviour.






Best regards,

Ioannis Vranos
 
I

Ioannis Vranos

Ioannis said:
and chars, signed chars (since stand alone signed chars can have the
value 0)


And in a strive for perfection let me rearrange my statement:


"memset() in general, can only be used to chars, signed chars and
unsigned chars, since these as stand alone types can have the value 0,
while any other type may contain bytes with padding bits (which means
bytes with non-zero values)."


Changing the value of padding bits produces undefined behaviour.






Best regards,

Ioannis Vranos
 
I

Ioannis Vranos

Rearranged again (for *last time* I promise!) :)


memset() in general, can only be used to chars, signed chars and
unsigned chars, since these as stand alone types can have the value 0,
while any other type may contain bytes with padding bits.

Changing the value of padding bits, even if it is the same value,
produces undefined behaviour.






Best regards,

Ioannis Vranos
 
J

JKop

Ioannis Vranos posted:
Changing the value of padding bits, even if it is the same value,
produces undefined behaviour.


Petting an animal has undefined behaviour. People still do it though.

I myself wouldn't pay a blind bit of notice to changing padding bits. Is
there an actual statement from the Standard that you can quote that
explicitly states your argument?


-JKop
 
I

Ioannis Vranos

JKop said:
Ioannis Vranos posted:




I myself wouldn't pay a blind bit of notice to changing padding bits. Is
there an actual statement from the Standard that you can quote that
explicitly states your argument?



As far as I know the standard mentions nothing regarding changing the
value of padding bits.

Perhaps a better terminology would be "system-dependent" behaviour.

In any case, it is *not* a well-defined behaviour.






Best regards,

Ioannis Vranos
 
I

Ioannis Vranos

Ioannis said:
As far as I know the standard mentions nothing regarding changing the
value of padding bits.

Perhaps a better terminology would be "system-dependent" behaviour.

In any case, it is *not* a well-defined behaviour.


.... in general. There are special cases where *the same value* of
padding bits can be written as in the case of POD types:


"For any complete POD object type T, whether or not the object holds a
valid value of type T, the underlying
bytes (1.7) making up the object can be copied into an array of char or
unsigned char.36) If the content
of the array of char or unsigned char is copied back into the object,
the object shall subsequently
hold its original value.

[Example:
#define N sizeof(T)
char buf[N];
T obj; // obj initialized to its original value
memcpy(buf, &obj, N); // between these two calls to memcpy,
// obj might be modified
memcpy(&obj, buf, N); // at this point, each subobject of obj of scalar type
// holds its original value
—end example]


3 For any POD type T, if two pointers to T point to distinct T objects
obj1 and obj2, if the value of obj1
is copied into obj2, using the memcpy library function, obj2 shall
subsequently hold the same value as
obj1. [Example:
T* t1p;
T* t2p;
// provided that t2p points to an initialized object ...
memcpy(t1p, t2p, sizeof(T)); // at this point, every subobject of POD
type in *t1p contains
// the same value as the corresponding subobject in *t2p
—end example]



4 The object representation of an object of type T is the sequence of N
unsigned char objects taken up by
the object of type T, where N equals sizeof(T). The value representation
of an object is the set of bits
that hold the value of type T. For POD types, the value representation
is a set of bits in the object representation
that determines a value, which is one discrete element of an
implementationdefined
set of values.37)"




However as it can be seen in the last paragraph, for non-POD types we
can only *read* the objects as arrays of unsigned chars.






Best regards,

Ioannis Vranos
 

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

No members online now.

Forum statistics

Threads
474,171
Messages
2,570,935
Members
47,472
Latest member
KarissaBor

Latest Threads

Top