asked    Darnell     2018-10-22       c       68 view        2 Answers

[SOLVED] Automatic storage duration struct initialization

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!

  2 Answers  

        answered    Jerry     2018-10-22      

Summary, TL;DR:

Storage duration explained:

  • A variable declared inside a function has automatic storage duration (including parameters to functions).
  • A variable declared as static, or a variable declared outside functions at file scope ("global") has static storage duration.

Struct (and array) initialization explained:

  • If you initialize no member and the struct has automatic storage duration, nothing is initialized.
  • If you initialize no member and the struct has static storage duration, all members are zero-initialized.
  • If you initialize any member(s), those you didn't touch get initialized to zero.

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:

  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?

No, it is not guaranteed; their values are indeterminate as stated in the first sentence of the quote above.

  1. 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.

  1. 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.



        answered    Blithe     2018-10-22      

First of all, uninitialized variables with automatic storage will never be initialized. From The C11 standard (ISO/IEC 9899:2011 §6.7.9/10):

If an object that has automatic storage duration is not initialized explicitly, its value is indeterminate.

Then from this structure initialization reference:

All members that are not initialized explicitly are initialized implicitly the same way as objects that have static storage duration.

And if we follow the "initialized implicitly" link we have:

objects with static and thread-local storage duration are initialized as follows

  • ...
  • objects of integral types are initialized to unsigned zero
  • ...

So to answer your questions:

  1. No, no initialization is done as there is no explicit initialization in your code (see the quote from the standard)
  2. Yes, since this is a structure initialization
  3. Yes, for same reason as 2.

The links provided have references to the standard.





Your Answer





 2018-10-22         Lauren

Check if field exists in CosmosDB JSON with SQL - nodeJS

I am using Azure CosmosDB to store documents (JSON).I am trying to query all documents that contain the field "abc", and not return the documents that do not have the field "abc". For example, return the first object below and not the second{ "abc": "123"}{ "jkl": "098"}I am trying to use the following code:client.queryDocuments(collectionUrl,`SELECT r.id, r.authToken.instagram,r.userName FROM root r WHERE r.abc`)I assumed the above would check if abc exists similar to if (r.abc) {}I have tried using WHERE r.abc IS NOT NULL Thanks in advance All you need to do is c...
 sql                     1 answers                     51 view
 2018-10-22         Bernice

Azure ARM template deployment

I am trying to create NIC card for VM through Azure ARM template. But my script also creates concats NIC to app gateway. But as of now I am only creating VM and NIC. Is there a condition to skip the Concatenation of App gateway and NIC. Below is the NIC creation template:{ "comments": "NIC_Creation", "tags": { "owner": "[parameters('owner')]" }, "condition": "[equals(parameters('CreateVM'), 'yes')]", "dependsOn": [ // "[resourceId('Microsoft.Network/publicIPAddresses', parameters('VM_PublicIP_Name'))]", // "[resourceId('Microsoft.Network/...
 azure                     2 answers                     50 view
 2018-10-22         Ives

Azure Active Directory with Springboot : how to handle expired oauth token?

I have a springboot app which use Microsoft Azure active directory to allow authentication (oauth2).I have followed the "how to" provided by Microsoft (https://docs.microsoft.com/en-us/java/azure/spring-framework/configure-spring-boot-starter-java-app-with-azure-active-directory?view=azure-java-stable).Everything is working well except that I have no ideas on how to handle expired token (after 1 hour) in a way that it will not affect the users.I know that it is possible to get new token with the refresh token but if I take a look in NimbusAuthorizationCodeTokenResponseClien...
 spring-boot                     1 answers                     49 view