Uninitialised fields in structures
- From: Ulrich Eckhardt <doomster@xxxxxxxx>
- Date: Mon, 31 Dec 2007 11:58:19 +0100
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
.
- Follow-Ups:
- Re: Uninitialised fields in structures
- From: Spiros Bousbouras
- Re: Uninitialised fields in structures
- From: vippstar
- Re: Uninitialised fields in structures
- Prev by Date: Re: Secure C programming
- Next by Date: Re: Secure C programming
- Previous by thread: function returning days of the week
- Next by thread: Re: Uninitialised fields in structures
- Index(es):
Relevant Pages
|