zero-filling gaps in statics

A

Andrey

What does the standard say about zero-initializing of static
structures/classes, specifically: is it guaranteed by the standard
that the alignment fillers between the members will also be
initialized to zero?


Andrey
 
V

Victor Bazarov

Andrey said:
What does the standard say about zero-initializing of static
structures/classes, specifically: is it guaranteed by the standard
that the alignment fillers between the members will also be
initialized to zero?

In 3.6.2 the standard says that "The storage for objects with static
storage duration (3.7.1) shall be zero-initialized (8.5) before any
other initialization takes place. Zero-initialization and
initialization with a constant expression are collectively called
static initialization;", so, to answer your question, yes, it is
guaranteed.

Victor
 
A

Andrew Koenig

What does the standard say about zero-initializing of static
structures/classes, specifically: is it guaranteed by the standard
that the alignment fillers between the members will also be
initialized to zero?

Suppose it did. How would you write a program to verify the
implementation's behavior that would be guaranteed to work on all
implementations?
 
A

Andrey

Andrew Koenig said:
Suppose it did. How would you write a program to verify the
implementation's behavior that would be guaranteed to work on all
implementations?

I suppose I could treat the sizeof(my_structure) of memory starting on
the structure's address as a character buffer and check if every byte
of it is zero. The background for my original question was that if a
statically initialized structure goes over the wire, sometimes I care
if I get random stuff in the bit stream on the other end. Normally,
perfect zero-filling is guaranteed by the loader, but I was curious if
this particular type of behavior actually made it to the language
specification, since loaders are rather OS-specific beasts.


Andrey
 
A

Andrew Koenig

I suppose I could treat the sizeof(my_structure) of memory starting on
the structure's address as a character buffer and check if every byte
of it is zero.

That strategy won't work, for at least two reasons:

1) The behavior of treating a pointer to a structure as if it were a
pointer to a character is implementation-defined. I don't think that there
is any place in the standard that requires the implementation to give you
the actual contents of that memory, even if you ask. The only guarantee is
that if you interpret a structure as a character string, and then turn that
same string back into a structure, you get the same results.

2) There is no guarantee that even the occupied bytes of a static
structure will be initialized to zero. For example:

struct X {
float f;
};

X x;

I'm assuming here that x is a static object. Now there is nothing in the
standard that requires a machine's floating-point representation to have all
of its bits zero--but the implementation is obliged to do whatever it takes
to give x.f an initial value of zero.

By implication, if you sere to interpret x as a sequence of characters,
there is no assurance that those characters will be zero. Why should there
be any corresponding assurance for chracters that are not part of a data
member?

If this second example is unconvincing, consider this one:

struct Y {
public: virtual void foo() { }
};

Y y;

I am not aware of *any* C++ implementation that will store y as a value with
all of its bits zero.
 
A

Andrey

1) The behavior of treating a pointer to a structure as if it were a
pointer to a character is implementation-defined. I don't think that there

Yes, I agree with this.
2) There is no guarantee that even the occupied bytes of a static
structure will be initialized to zero. For example:

This is correct, but I was only considering plain structures with no
control stuff in them (like V-tables). The case of floats is also
quite clear and was not of my concern. For the occupied bytes there are
constructors that take care of the initialization, even the static one.
So, you know very well what you're getting there: in the bytes occupied
by a float you'd get float(0), whatever bit sequence that might be.
The alignment gaps, on the other hand, are sort of `out of the scope
of the language', and there is almost nothing about them in the standard.


Andrey
 
A

Andrew Koenig

The alignment gaps, on the other hand, are sort of `out of the scope
of the language', and there is almost nothing about them in the standard.

Exactly.
 

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
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top