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 | | | | re: zero-filling gaps in statics
"Andrey" <rsanddx@gmail.com> wrote in message
news:a99087b.0406221021.69a20ae5@posting.google.co m...
[color=blue]
> 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?[/color]
Suppose it did. How would you write a program to verify the
implementation's behavior that would be guaranteed to work on all
implementations? | | | | re: zero-filling gaps in statics
Andrey wrote:[color=blue]
> 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?[/color]
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 | | | | re: zero-filling gaps in statics
"Andrew Koenig" <ark@acm.org> wrote in message news:<rU_Bc.116057$Gx4.76688@bgtnsc04-news.ops.worldnet.att.net>...[color=blue]
> "Andrey" <rsanddx@gmail.com> wrote in message
> news:a99087b.0406221021.69a20ae5@posting.google.co m...
>[color=green]
> > 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?[/color]
>
> Suppose it did. How would you write a program to verify the
> implementation's behavior that would be guaranteed to work on all
> implementations?[/color]
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 | | | | re: zero-filling gaps in statics
"Andrey" <rsanddx@gmail.com> wrote in message
news:a99087b.0406230818.17596345@posting.google.co m...
[color=blue]
> 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.[/color]
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. | | | | re: zero-filling gaps in statics
> 1) The behavior of treating a pointer to a structure as if it were a[color=blue]
> pointer to a character is implementation-defined. I don't think that there[/color]
Yes, I agree with this.
[color=blue]
> 2) There is no guarantee that even the occupied bytes of a static
> structure will be initialized to zero. For example:[/color]
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 | | | | re: zero-filling gaps in statics
"Andrey" <rsanddx@gmail.com> wrote in message
news:a99087b.0406240650.3580200@posting.google.com ...
[color=blue]
> 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.[/color]
Exactly. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,223 network members.
|