#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2

    Declaring incomplete structs


    By "incomplete", I mean a declaration in which some components are not mentioned. (not even initialized)
    I tried to do this, and worked.

    Here's the relevant code:
    Code:
    typedef struct
    {
        double voltage, capacity, stored;
    } battery_t;
    
    battery_t x = {12, 5e6};
    This worked, but I want to know if I just got lucky or if this is a legitimate declaration.

    (I then wondered if declaring incomplete arrays (incomplete having the same meaning above, not whether the size is stated or not) was possible.
    But then I realized strings are precisely these incomplete arrays, so I concluded such declarations, at least for arrays, are in fact legitimate.)
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,163
    Rep Power
    2222
    Very sloppy wording. Your structure is not incomplete; it has been declared completely and fully.

    Rather, it's your initialization of that structure that is not complete, meaning that you did not explicitly initialize each and every field in the structure.

    From The Annotated ANSI C Standard, Section 6.5.7 Initialization:
    Originally Posted by ANSI/ISO 9899-1990
    If there are fewer initializers in a brace-enclosed list than there are members of an aggregate, then the remainder of the aggregate shall be initialized implicitly the same as objects that have static storage duration.
    IOW, the values in the initialization list are used on the first members of the aggregate (eg, fields in a struct, elements in an array) and the rest are set to zero.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2
    Very sloppy wording.
    You're right, I got declaration and initialization mixed up.
    Thanks for pointing it out.

    initialized implicitly the same as objects that have static storage duration.
    I'm unclear about the word "static storage duration".
    I googled it, and have the understanding that these objects retain their values from the beginning of the program to the end of the program.

    Does it mean that the remainder objects would be allocated memory in the same manner as when I hadn't initialized at all?
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,163
    Rep Power
    2222
    Originally Posted by 046
    I'm unclear about the word "static storage duration".

    I googled it, and have the understanding that these objects retain their values from the beginning of the program to the end of the program.
    Yes, that is correct.

    There are two storage durations: static and automatic. There are different ways in which a program can implement the two, which depends very much on the hardware and operating system of the computer it's running on. Because we are most familiar with microprocessor-based personal computers, we tend to describe static duration as using the read/write memory allotted to the program and automatic duration as using the stack.

    More specifically, static storage normally applies to variables that are declared as "global variables" outside of a function and hence having file scope. Automatic stoarge normally applies to "local variables" that are declared inside of a function and only has function scope.

    Thus, as you had researched, the storage for a static variable is created during the program's start-up before main() even gets called and continues to exist until after you have returned from main(). The storage for an auto variable is created on the stack when its function is being called and it ceases to exist when you return from the function, so it only exists as long as the function is still in effect (eg, the function won't actually be running when a function that it has called is running, but it will still be on the stack).

    A minor exception to this rule is if you declare an auto variable to be static. When this happens, the variable still only has function scope, so it can only be accessed within the function where it is declared, but it will be stored along with all the other static variables and hence will exist with static duration, for the duration of the program.

    Originally Posted by 046
    Does it mean that the remainder objects would be allocated memory in the same manner as when I hadn't initialized at all?
    That has nothing to do with static vs auto duration. Rather, the normal rules of implicit initialization would apply:
    1. static duration variables are implicitly initialized to zero.
    2. auto duration variables are not implicitly initialized and hence will contain garbage.

    Now, let's assume this declaration:
    Code:
    int main()
    {
        int intAry[42];
    intAry is a local array, so there would be no implicit initialization. It starts out containing garbage, which will only change for the elements that we assign values to in the code.

    Now let's just initialize the first element:
    Code:
    int main()
    {
        int intAry[42] = {0};
    You have initialized intAry[0] explicitly and the remain 41 elements are initialized to zero implicitly. It doesn't not contain any garbage, but rather all zeros. If you had initialized intAry[0] to 80, then intAry[0] would be 80 and all the remaining elements would contain zero.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    142
    Rep Power
    2
    I see that I didn't understand initializations were different for each type of storage duration, which is explained here in your post:
    1. static duration variables are implicitly initialized to zero.
    2. auto duration variables are not implicitly initialized and hence will contain garbage.
    I did an experiment to verify the different manners of initialization.
    For the program
    Code:
    #include <stdio.h>
    #include <string.h>
    #define SIZE 5
    
    int global[SIZE];
    
    int main()
    {
        int local1[SIZE];
        int local2[SIZE] = {80};
    
        int i;
    
        printf("static variable: ");
        for(i = 0; i < SIZE; ++i)
        {
            printf("%d ", global[i]);
        }
    
        printf("\nno implicit initialization: ");
        for(i = 0; i < SIZE; ++i)
        {
            printf("%d ", local1[i]);
        }
    
        printf("\nwith implicit initialization: ");
        for(i = 0; i < SIZE; ++i)
        {
            printf("%d ", local2[i]);
        }
    }
    I got

    Code:
    static variable: 0 0 0 0 0
    no implicit initialization: 2293744 4200878 4200784 3342445 6029362
    with implicit initialization: 80 0 0 0 0

IMN logo majestic logo threadwatch logo seochat tools logo