Re: Uninitialised fields in structures
- From: vippstar@xxxxxxxxx
- Date: Mon, 31 Dec 2007 03:34:06 -0800 (PST)
On Dec 31, 12:58 pm, Ulrich Eckhardt <dooms...@xxxxxxxx> wrote:
Greetings!That is true only for an array.
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 anThe standard doesn't say anything about initializing the rest members
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?
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'.
.
- Follow-Ups:
- Re: Uninitialised fields in structures
- From: Ulrich Eckhardt
- Re: Uninitialised fields in structures
- From: Ben Bacarisse
- Re: Uninitialised fields in structures
- References:
- Uninitialised fields in structures
- From: Ulrich Eckhardt
- Uninitialised fields in structures
- Prev by Date: Re: Secure C programming
- Next by Date: Re: Cannot compile with _FILE_OFFSET_BITS = 64
- Previous by thread: Uninitialised fields in structures
- Next by thread: Re: Uninitialised fields in structures
- Index(es):
Relevant Pages
|