Saturday, 15 September 2012

c - Unsigned int field of struct got automatically initialized? -



c - Unsigned int field of struct got automatically initialized? -

does c guarantees unsigned integer field within struct gets initialized zero? in system, seems (or "lucky"(actually unlucky)).

in code words, happen in next scenario?

struct node { unsigned int rec_size; }; struct node node; // value of node.rec_size? undefined or 0?

relevant answer, not same, since in example, there 1 field , no initialization.

the reply is, depends on storage-class:

if _thread_local or static, guaranteed zeroed.

if auto or dynamic storage, no initialization takes place.

6.7.9 initialization

10 if object has automatic storage duration not initialized explicitly, value indeterminate. if object has static or thread storage duration not initialized explicitly, then:

if has pointer type, initialized null pointer; if has arithmetic type, initialized (positive or unsigned) zero; if aggregate, every fellow member initialized (recursively) according these rules, , padding initialized 0 bits; if union, first named fellow member initialized (recursively) according these rules, , padding initialized 0 bits;

btw: zero-initialization in c++ equivalent rules static/thread-local objects.

c struct

No comments:

Post a Comment