Re: Uninitialised fields in structures
- From: Spiros Bousbouras <spibou@xxxxxxxxx>
- Date: Mon, 31 Dec 2007 05:02:56 -0800 (PST)
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 ? ;-)
.
- References:
- Uninitialised fields in structures
- From: Ulrich Eckhardt
- Uninitialised fields in structures
- Prev by Date: Re: Cannot compile with _FILE_OFFSET_BITS = 64
- Next by Date: Re: Uninitialised fields in structures
- Previous by thread: Re: Uninitialised fields in structures
- Next by thread: Redirect stdin ?
- Index(es):
Relevant Pages
|