Re: Uninitialised fields in structures



vippstar@xxxxxxxxx writes:

On Dec 31, 12:58 pm, Ulrich Eckhardt <dooms...@xxxxxxxx> wrote:
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).
<snip>
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.

I think it does. 6.7.8 para. 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.

Further up (para. 10) explains that objects of static duration are
initialised to arithmetic zero or NULL by default.

--
Ben.
.



Relevant Pages

  • 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)
  • Re: Initialising Variables
    ... automatically zero out global memory over and above objects of lesser ... It's naturally implementable on common machines and operating systems: ... heap, there is no reason to initialise it at all, the data in there is ...
    (comp.lang.c)
  • Re: FindFirst faArchive?
    ... Do people generally initialise an integer, or any cardinal to zero ... Object fields, variables and properties are initialised to zero ... also initialised to zero by Delphi. ...
    (comp.lang.pascal.delphi.misc)