Re: Uninitialised fields in structures
- From: Ben Bacarisse <ben.usenet@xxxxxxxxx>
- Date: Mon, 31 Dec 2007 13:06:33 +0000
vippstar@xxxxxxxxx writes:
On Dec 31, 12:58 pm, Ulrich Eckhardt <dooms...@xxxxxxxx> wrote:<snip>
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).
Now, I'm pretty sure about the rule with the additional fields, but I'mThe standard doesn't say anything about initializing the rest members
wondering nonetheless. Can someone confirm or deny whether t.len above is
initialised or not?
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.
.
- References:
- Uninitialised fields in structures
- From: Ulrich Eckhardt
- Re: Uninitialised fields in structures
- From: vippstar
- Uninitialised fields in structures
- Prev by Date: Re: Uninitialised fields in structures
- Next by Date: Re: function reading hour register either in 12 or 24 hour formats and returning properly
- Previous by thread: Re: Uninitialised fields in structures
- Next by thread: Re: Uninitialised fields in structures
- Index(es):
Relevant Pages
|