Why does gcc allow extern declarations of type void (non-pointer)?

前端 未结 4 1413
广开言路
广开言路 2020-12-16 11:26

Why does gcc allow extern declarations of type void? Is this an extension or standard C? Are there acceptable uses for this?

I am guessing it is an extension, but

4条回答
  •  温柔的废话
    2020-12-16 12:17

    I've found the only legitimate use for declaring

    extern void foo;
    

    is when foo is a link symbol (an external symbol defined by the linker) that denotes the address of an object of unspecified type.

    This is actually useful because link symbols are often used to communicate the extent of memory; i.e. .text section start address, .text section length, etc.

    As such, it is important for the code using these symbols to document their type by casting them to an appropriate value. For instance, if foo is actually the length of a memory region:

    uint32_t textLen;
    
    textLen = ( uint32_t )foo;
    

    Or, if foo is the start address of that same memory region:

    uint8_t *textStart;
    
    textStart = ( uint8_t * )foo;
    

    The only alternate way to reference a link symbol in "C" that I know of is to declare it as an external array:

    extern uint8_t foo[];
    

    I actually prefer the void declaration, as it makes it clear that the linker defined symbol has no intrinsic "type."

提交回复
热议问题