Re: Uninitialised fields in structures



On Dec 31, 10:58 am, Ulrich Eckhardt <dooms...@xxxxxxxx> wrote:
Greetings!

I was recently surprised by the compiler's warning concerning this code:

  struct text {
    char* s;
    size_t len;
  };
  int main() {
    struct text t = {"hello world!"};
  }

The compiler actually claimed that t.len was uninitialised. Okay, I don't
explicitly initialise it, but I was under the impression that it should be
initialised to zero then (i.e. all fields after the last one are
initialised with zero).

Indeed. Paragraph 21 of 6.7.8 of n1256 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.

and paragraph 10 of the same clause states that
arithmetic types of static storage are initialized to 0.

Okay, it's just a warning, so I tended to ignore
it. Now, when I ran the code through Valgrind, it also complained that an
uninitialised value was used, which got me thinking.

Regarding the compiler perhaps it just warns you about
the lack of explicit initialization. Regarding valgrind I
don't know how it deals with such issues.

Now, I'm pretty sure about the rule with the additional fields, but I'm
wondering nonetheless. Can someone confirm or deny whether t.len above is
initialised or not?

According to the standard it should. Perhaps the person
who wrote your compiler was ignorant about that part of
the standard ? ;-)
.



Relevant Pages

  • Re: Interview Embedded Software Questions
    ... It is not what I understand it is what the standard says. ... "There is absolutely no requirement for a compiler to generate run-time ... computation to initialise automatic variables - but it must act as ...
    (comp.arch.embedded)
  • Re: why doesnt this work? final assignments and switches
    ... > public foo() ... expressions, nothing else is allowed. ... first you need to initialise them explicitly and to constant valued ... BTW, this isn't about "how clever" the compiler is, or should be. ...
    (comp.lang.java.programmer)
  • Re: Interview Embedded Software Questions
    ... int x=1; ... The compiler doesn't have to be broken at all. ... > computation to initialise automatic variables - but it must act as ... I know how automatic variables work, ...
    (comp.arch.embedded)
  • Re: Attempt at initialising a class with a vector "inline"
    ... > I am trying to write code that allows me to initialise one of my ... > inline. ... > int main{ ... The compiler tries to parse this as: ...
    (comp.lang.cpp)
  • Re: Is this legal?
    ... but my compiler doesn't do this right - I already knew that. ... That it is the only way to initialise an 'const' ... constructer has completed constructing the object, ...
    (alt.comp.lang.learn.c-cpp)