471,092 Members | 1,027 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,092 software developers and data experts.

zero-filling gaps in statics

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
Jul 22 '05 #1
6 1526
"Andrey" <rs*****@gmail.com> wrote in message
news:a9*************************@posting.google.co m...
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?
Jul 22 '05 #2
Andrey wrote:
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
Jul 22 '05 #3
"Andrew Koenig" <ar*@acm.org> wrote in message news:<rU********************@bgtnsc04-news.ops.worldnet.att.net>...
"Andrey" <rs*****@gmail.com> wrote in message
news:a9*************************@posting.google.co m...
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?


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
Jul 22 '05 #4
"Andrey" <rs*****@gmail.com> wrote in message
news:a9*************************@posting.google.co m...
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.
Jul 22 '05 #5
> 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
Jul 22 '05 #6
"Andrey" <rs*****@gmail.com> wrote in message
news:a9************************@posting.google.com ...
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.
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Steven T. Hatton | last post: by
28 posts views Thread by Andreas Prilop | last post: by
25 posts views Thread by Mantorok Redgormor | last post: by
15 posts views Thread by Tomás | last post: by
23 posts views Thread by Hallvard B Furuseth | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.