Structure initialization

J

jb.simon

Recently I was pinged in a code review about my use of the
initialization method
AStruct myStruct = { 0 } ;

Which initializes all elements of the myStruct to 0.

I was questioned on it because no one had seen this construct.
So i looked it up as best i could in the pointers to the Specification
that I find here and could not find any specific reference to this
construct. What i did find was reference to (paraphrasing) any values
not specified in the initializer are initialized to zero.

so it seems that the construct ={0}, sets the fisrst element to 0 and
since there are no more initializers present, all other structure
members are set to zero by default. Is this correct ?

Thanks
Joe
 
J

John Bode

Recently I was pinged in a code review about my use of the
initialization method
AStruct myStruct = { 0 } ;

Which initializes all elements of the myStruct to 0.

I was questioned on it because no one had seen this construct.
So i looked it up as best i could in the pointers to the Specification
that I find here and could not find any specific reference to this
construct. What i did find was reference to (paraphrasing) any values
not specified in the initializer are initialized to zero.

so it seems that the construct ={0}, sets the fisrst element to 0 and
since there are no more initializers present, all other structure
members are set to zero by default. Is this correct ?

Thanks
Joe

Sort of. Here's the language from n1124:

"6.7.8. 21 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."

And the section on initialization of static objects:

"10 If an object that has automatic storage duration is not
initialized explicitly,
its value is indeterminate. If an object that has static storage
duration is not
initialized explicitly, then:
— if it has pointer type, it is initialized to a null pointer;
— if it has arithmetic type, it is initialized to (positive or
unsigned) zero;
— if it is an aggregate, every member is initialized (recursively)
according to these rules;
— if it is a union, the first named member is initialized
(recursively) according to
these rules."
 
W

Walter Roberson

Recently I was pinged in a code review about my use of the
initialization method
AStruct myStruct = { 0 } ;
Which initializes all elements of the myStruct to 0.
I was questioned on it because no one had seen this construct.

It does what you expected, and is, I believe, recognized by
experienced C programmers.

On the other hand, code reviews are often useful to catch constructs
that might be unclear to the student programmer who is going to
be asked to work on the code several years from now.

In my opinion, the code is correct and there is no value in changing
the code to explicitly initialize all the values "just in case" someone
doesn't understand the code; the explicit initialization would just
become a source of potential errors. But for future code maintenance,
it wouldn't hurt to put in a comment indicating that the entire
structure was being initialized; the comment code include a reference
to the specific section of the Standard for those who might be
a little too convinced that the code is mistaken.
 
Y

Yunzhong

Recently I was pinged in a code review about my use of the
initialization method
AStruct myStruct = { 0 } ;

Which initializes all elements of the myStruct to 0.

I was questioned on it because no one had seen this construct.
So i looked it up as best i could in the pointers to the Specification
that I find here and could not find any specific reference to this
construct. What i did find was reference to (paraphrasing) any values
not specified in the initializer are initialized to zero.

so it seems that the construct ={0}, sets the fisrst element to 0 and
since there are no more initializers present, all other structure
members are set to zero by default. Is this correct ?

Thanks
Joe


I think you are right. The only "exception" I can think of is that
pointer members will be initialized to a null pointer constant which
may not be all-bit zero, but maybe a null pointer constant can be
considered as just a special kind of zero.
 
A

Andrey Tarasevich

Recently I was pinged in a code review about my use of the
initialization method
AStruct myStruct = { 0 } ;

Which initializes all elements of the myStruct to 0.

I was questioned on it because no one had seen this construct.
So i looked it up as best i could in the pointers to the Specification
that I find here and could not find any specific reference to this
construct. What i did find was reference to (paraphrasing) any values
not specified in the initializer are initialized to zero.

so it seems that the construct ={0}, sets the fisrst element to 0 and
since there are no more initializers present, all other structure
members are set to zero by default. Is this correct ?
...

Yes, that's absolutely correct (with the exception of _unnamed_ members,
which are left uninitialized). The construct you used is a
well-recognized zero-initialization idiom in C.

The fact that "no one had seen this construct" in your team simply means
that other members haven't learned it yet.
 
H

Harald van Dijk

so it seems that the construct ={0}, sets the fisrst element to 0 and
since there are no more initializers present, all other structure
members are set to zero by default. Is this correct ?

That is correct. And when the first member is an array or structure (or a
union starting with one of these), it applies to the remaining members or
elements of the first member as well. That is, when X is a simple integer
constant, possibly 0, possibly something else,

struct A {
struct B {
int a, b, c;
} s;
int d;
} a = {X};

initialises a.s.a to X, while a.s.b, a.s.c, and a.d are all given their
default values of 0.
 
S

santosh

pete wrote:

If the only reason that a programmer
can't understand a particular C code construct,
is because the programmer doesn't know enough C,
then it's time to learn more C.

Only if your boss gives you time off to do so.

<snip>
 
W

Walter Roberson

pete said:
If the only reason that a programmer
can't understand a particular C code construct,
is because the programmer doesn't know enough C,
then it's time to learn more C.
What kind of a programmer needs to be told that?
Are child labor laws no longer being enforced?

When it comes to programming... No, they aren't.

Some of my programming-related skills are -relatively- rare
even amongst programmers, and as a result I earn a decent
(but not rich) living from my work. But I grew up in an era
when my father literally maintained *mechanical* calculators
(and manual typewriters and non-programmable electric typewriters),
together with a few early electronic calculators with very basic
functionality and Nixie tubes (the mechanical calculators had
more functionality!). My nieces and nephews are growing up in
an era when it isn't uncommon for a five-year-old to be better at
using computers than their parents.

High school computer multi-millionares are a reality: the young have a
grasp of the potential (and the likely social desirability) of computer
systems that I will likely never have. So Yes, children *are*
programming these days, and are producing some amazing things.


But beyond that... there will always be a steady stream of the
marginally competant who don't know enough to look in a reference book.
And if the university students I often see posting in another technical
newsgroup are typical of the modern generation, then it appears that
we have somehow managed to raise a generation of programmers who
consider it to be beneath them to do any research, convinced that
they are *owed* complete code to do <whatever>, annoyed that they
even have to ask once for it... and yet convinced that whatever they
"know" is right and anything contra-indicative of what they "know"
is wrong (or was *deliberately* mis-designed.) A generation of
Entitlement to other people's skills and labour, and one that
too often has an antipathy to learning. It is a generation that
is proving surprisingly difficult to teach. :(

Thus, including a comment with a reference to the Standard can be
form of self-defence: the barbarians likely will not stop to -read-
the Standard before complaining about your code, but you can
then respond, "And what did that section of the Standard say
about the situation??" instead of having to spend your time writing
memos pointing out the relevant section, and spending your time
defending your programming style, and being written up as
"not a team player" and whatever.
 
K

Keith Thompson

Richard Heathfield said:
Yunzhong said:

It doesn't matter - it's not an exception regardless of the representation
of a null pointer. The static default initialisers that apply here are 0
for integer types, 0.0 for floating point types, and NULL for pointer
types.

Yes.

Another way to look at it is that the default initializer is simply 0
for *all* scalar types, converted in each case to the appropriate
type.

[...]
 
S

santosh

pete said:
Do I understand you to mean
that if your boss saw you reading a C reference
while you were supposed to be writing C code,
that your boss would be displeased?

That notion seems bizarre to me.
What do you really mean?

I mean that here, outside of transnational companies, it's very unusual
in the middle to low end jobs here for your "boss" to let you take time
off, even if it might benefit your work efficiency in the long run.

Specifically, with regards to C, clc's standards are, IME _very_ high.
For example in the code bases I have had the opportunity to examine I
have never once come across the form of structure initialisation
demonstrated in this thread.
 

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

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,235
Latest member
Top Crypto Podcasts_

Latest Threads

Top