Uninitialised fields in structures



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). 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. Lastly, I used gdb to
step through the code and explicitly shredded the value of t.len before
that line and - lo and behold - it was correctly (IMHO) reset to zero!

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?

thank you

Uli

.



Relevant Pages

  • Re: Uninitialised fields in structures
    ... I was recently surprised by the compiler's warning concerning this code: ... explicitly initialise it, but I was under the impression that it should be ... initialised with zero). ... Using gdb to determine whether a program is correct or not is bad ...
    (comp.lang.c)
  • Re: Uninitialised fields in structures
    ... I was recently surprised by the compiler's warning concerning this code: ... The compiler actually claimed that t.len was uninitialised. ... explicitly initialise it, but I was under the impression that it should be ... initialised with zero). ...
    (comp.lang.c)
  • Re: Separator Line in Toolbox menus
    ... > initialised if you want their value to be zero. ... but its is good practice to explicitly initialise ... all variables of any type and scope. ...
    (comp.sys.acorn.programmer)
  • Re: Interview question: variable initialisation
    ... system which doesn't initialise all global memory at the start. ... the startup code _might_ initialize all of the RAM to zero ... that all uninitialized static lifetime variables in C are initialized ... apply to 'auto' variables, which are usually stack-based and do not ...
    (comp.arch.embedded)
  • Re: Interview question: variable initialisation
    ... system which doesn't initialise all global memory at the start. ... the startup code _might_ initialize all of the RAM to zero ... that all uninitialized static lifetime variables in C are initialized ... apply to 'auto' variables, which are usually stack-based and do not ...
    (comp.arch.embedded)