Can the address of a variable with automatic storage duration be taken in its definition?

前端 未结 3 1907
不知归路
不知归路 2021-01-14 17:08

Is it allowed to take the address of an object on the right hand-side of its definition, as happens in foo() below:

typedef struct { char x[100]         


        
相关标签:
3条回答
  • 2021-01-14 17:23

    6.2.1 Scopes of identifiers

    1. Structure, union, and enumeration tags have scope that begins just after the appearance of the tag in a type specifier that declares the tag. Each enumeration constant has scope that begins just after the appearance of its defining enumerator in an enumerator list. Any other identifier has scope that begins just after the completion of its declarator.

    In

    chars b = make(&b);
    //    ^^
    

    the declarator is b, so it is in scope in its own initializer.

    6.2.4 Storage durations of objects

    1. For such an [automatic] object that does not have a variable length array type, its lifetime extends from entry into the block with which it is associated until execution of that block ends in any way.

    So in

    { // X
      chars b = make(&b);
    }
    

    the lifetime of b starts at X, so by the time the initializer executes, it is both alive and in scope.

    As far as I can tell, this is effectively identical to

    {
      chars b;
      b = make(&b);
    }
    

    There's no reason you couldn't use &b there.

    0 讨论(0)
  • 2021-01-14 17:23

    The question has already been answered, but for reference, it doesn't make much sense. This is how you would write the code:

    typedef struct { char x[100]; } chars;
    
    chars make (void) {
      chars c;
      /* init c */
      return c;
    }
    
    void foo(void) {
      chars b = make();
    }
    

    Or perhaps preferably in case of an ADT or similar, return a pointer to a malloc:ed object. Passing structs by value is usually not a good idea.

    0 讨论(0)
  • 2021-01-14 17:28

    To answer the question in the title, with your code sample in mind, yes it may. The C standard says as much in §6.2.4:

    The lifetime of an object is the portion of program execution during which storage is guaranteed to be reserved for it. An object exists, has a constant address, and retains its last-stored value throughout its lifetime.

    For such an object that does not have a variable length array type, its lifetime extends from entry into the block with which it is associated until execution of that block ends in any way.

    So yes, you may take the address of a variable from the point of declaration, because the object has the address at this point and it's in scope. A condensed example of this is the following:

    void *p = &p;
    

    It serves very little purpose, but is perfectly valid.

    As for your second question, what can you do with it. I can mostly say I wouldn't use that address to access the object until initialization is complete, because the order of evaluation for expressions in initializers is left unsepcified (§6.7.9). You can easily find your foot shot off.

    One place where this does come through, is when defining all sorts of tabular data structures that need to be self referential. For instance:

    typedef struct tab_row {
      // Useful data
      struct tab_row *p_next;
    } row;
    
    row table[3] = {
      [1] = { /*Data 1*/, &table[0] },
      [2] = { /*Data 2*/, &table[1] },
      [0] = { /*Data 0*/, &table[2] },
    };
    
    0 讨论(0)
提交回复
热议问题