Re: Uninitialised fields in structures



On Dec 31, 12:58 pm, 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). Okay, it's just a warning, so I tended to ignore
That is true only for an array.
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?
The standard doesn't say anything about initializing the rest members
to 0.
Using gdb to determine whether a program is correct or not is bad
practise, as it is to run the program and come to a conclusion by the
output.
Gdb is not a 'C99 tool'.
.



Relevant Pages

  • 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). ...
    (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: 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)
  • Re: Separator Line in Toolbox menus
    ... It can save all sorts of problems if you later change the scope of a variable to one thats not implicitly zero initialised. ... adviced not to initialise static variables if their value was 0 just to avoid potentional impact on binary size for all the different compilers they are and will be using. ... Does someone know if zero initialised areas of C ROM components are created during runtime or are flattened in ROM space during build/link time? ... Implicitly or explictly zero value static variables 'small' in size are placed in a r/w data area while the larger onces are placed in a zero initialised area. ...
    (comp.sys.acorn.programmer)