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


Does C guarantee that an unsigned integer area inside a structure is started at zero? In my system, it seems (or I'm very "lucky" (actually unlucky).)

In code words, what will happen in the following scenario?

  structure node {unsigned int rec_size; }; Structure node node; // what is the value of node .rec_size? Undefined or 0?  

Relevant, but not so, because in my example, there is only one field and there is no initialization.

The answer depends on the storage category:

If it is < Code> _Thread_local or is static

If it is auto or dynamic storage, then no one starts.

6.7.9 Initialization

10 If the time of automatic storage has not been explicitly started in an object, its value is uncertain if an object that contains constant If the thread is a storage period then it is not explicitly started, then:

  • If there is an indicator type in it, then it is started for a null pointer;
  • If this is an arithmetic type, this (positive or unsigned) is started at zero;
  • If it is a composite, then each member is initially started according to these rules, and any padding is started on zero bits;
  • If it is a union, then the first nominated member is initially started according to these rules, and any padding is started on zero bits;

BTW: Zero-initialize in C ++ is equal to those rules for stable / thread-local objects.


Comments

Popular posts from this blog

c# - NewtonSoft JArray - how to select multiple elements with LINQ -

c# - Process.Kill() returns access denied -

c# - Using the generic type 'System.Collections.Generic.List<T>' requires 1 type arguments -