Some of this may be a duplicate, but I am sorry for that.
Let's say I have this struct
:
struct foo
{
int a;
int b;
int c;
};
1. If struct foo
type object is declared in the way that it has automatic storage duration and without initializers, is it guaranteed that all it's members will be force initialized to zero?
{
// other stuff
struct foo bar;
// other stuff
}
2. If struct foo
type object is declared in the way that it has automatic storage duration and with some initializers, is it guaranteed that members, which are not explicitly initialized, will be force initialized to zero?
{
// other stuff
struct foo bar = {.a = 1};
// other stuff
}
3. If struct foo
type object is declared in the way that it has automatic storage duration and by using compound literal expression, is it guaranteed that members, which are not explicitly initialized, will be force initialized to zero?
{
// other stuff
func((struct foo){.a = 1});
// other stuff
}
Any C standard references are much appreciated! Thank you!
Summary, TL;DR:
Storage duration explained:
static
, or a variable declared outside functions at file scope ("global") has static storage duration. Struct (and array) initialization explained:
The relevant part of the C standard (C17 6.7.9 §10):
If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate. If an object that has static or thread storage duration is not initialized explicitly, then:
- if it has pointer type, it is initialized to a null pointer;
- if it has arithmetic type, it is initialized to (positive or unsigned) zero;
- if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;
Where "artihmetic type" is standard gibberish for plain variables like int
, and "aggregate" is standard gibberish for arrays and structs.
Further down in the same chapter (C17 6.7.9 §19):
...all subobjects that are not initialized explicitly shall be initialized implicitly the same as objects that have static storage duration.
Answers to your questions:
- If struct foo type object is declared in the way that it has automatic storage duration and without initializers, is it guaranteed that all it's members will be force initialized to zero?
No, it is not guaranteed; their values are indeterminate as stated in the first sentence of the quote above.
- If struct foo type object is declared in the way that it has automatic storage duration and with some initializers, is it guaranteed that members, which are not explicitly initialized, will be force initialized to zero?
Yes, as per C17 6.7.9 §19 cited above.
- If struct foo type object is declared in the way that it has automatic storage duration and by using compound literal expression, is it guaranteed that members, which are not explicitly initialized, will be force initialized to zero?
Yes, since compound literals are arrays or structs, they follow the same initialization rules.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With