Question Regarding Static Initialisation of Aggregates.

J

John Smith

Hi,

I have a question regarding the initialisation of aggregates:

The C (99) standard states:
section 6.7.8, paragraph 21 states:
If there are fewer initializers in a brace-enclosed list than there are
elements or members of an aggregate, or fewer characters in a string
literal used to initialize an array of known size than there are elements
in the array, the remainder of the aggregate shall be
initialized implicitly the same as objects that have static storage
duration.

Does "remainder" here refer to padding as well or just field types (for
aggregates)? My experiments with gcc seem to indicate padding is zeroed as
well but I just want to confirm.

Thanks
 
V

Vladimir S. Oka

John said:
Hi,

I have a question regarding the initialisation of aggregates:

The C (99) standard states:
section 6.7.8, paragraph 21 states:
If there are fewer initializers in a brace-enclosed list than there are
elements or members of an aggregate, or fewer characters in a string
literal used to initialize an array of known size than there are elements
in the array, the remainder of the aggregate shall be
initialized implicitly the same as objects that have static storage
duration.

Does "remainder" here refer to padding as well or just field types (for
aggregates)? My experiments with gcc seem to indicate padding is zeroed as
well but I just want to confirm.

I don't think there's any requirement on initialising padding elements
at all. I'd asume their value is random. I guess you may rely on
(hopefully documented) behaviour of a particular compiler, but would
lose portability in the process.
 
F

Flash Gordon

John said:
Hi,

I have a question regarding the initialisation of aggregates:

The C (99) standard states:
section 6.7.8, paragraph 21 states:
If there are fewer initializers in a brace-enclosed list than there are
elements or members of an aggregate, or fewer characters in a string
literal used to initialize an array of known size than there are elements
in the array, the remainder of the aggregate shall be
initialized implicitly the same as objects that have static storage
duration.

The above is talking about members of the aggregate. Padding is space
between the members, it is not members. So padding may not be
initialised to 0.
Does "remainder" here refer to padding as well or just field types (for
aggregates)? My experiments with gcc seem to indicate padding is zeroed as
well but I just want to confirm.

There is no guarantee. It could initialise the padding bytes or leave
them untouched. gcc is probably initialising them because that was
either the easy or the efficient way to do it on your platform.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
 

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,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top