【问题标题】:Extern and Static Pointers in CC中的外部和静态指针
【发布时间】:2010-11-20 04:00:34
【问题描述】:

嗨,静态和外部指针的用法是什么?如果它们存在

【问题讨论】:

    标签: c pointers static extern


    【解决方案1】:

    为了回答您关于何时可以使用它们的问题,举几个简单的例子:

    静态指针可用于实现一个函数,该函数总是向程序返回相同的缓冲区,并在第一次调用时分配它:

    char * GetBuffer() {
       static char * buff = 0;
       if ( buff == 0 ) {
           buff = malloc( BUFSIZE );
       }
       return buff;
    }
    

    外部(即全局)指针可用于允许其他编译单元访问 main 的参数:

    extern int ArgC = 0;
    extern char ** ArgV = 0;
    
    int main( int argc, char ** argv ) {
       ArgC = argc;
       ArgV = argv;
       ...
    }
    

    【讨论】:

    • 为什么buff是char?不能是int吗?
    • 它是 char 因为它是一个例子。可以是 int、double、yada yada。
    【解决方案2】:

    简短的回答:它们不存在。 C99 6.7.1 说“最多可以在声明中的声明说明符中给出一个存储类说明符”。 externstatic 都是存储类说明符。

    【讨论】:

    • 这回答了对这个问题的令人惊讶的解释。我希望这个问题的意思是:静态指针的用途是什么,外部指针的用途是什么(因为我知道两者都存在,所以我会忽略这个限定)。
    【解决方案3】:

    假设我有一个指针,我想让多个翻译单元全局可用。我想在一个地方(foo.c)定义它,但允许在其他翻译单元中对其进行多个声明。 “extern”关键字告诉编译器这不是对象的定义声明;实际定义将出现在别处。它只是使链接器可以使用对象名称。编译和链接代码时,所有不同的翻译单元都将使用该名称引用同一个对象。

    假设我也有一个指针,我确实想让单个源文件中的函数全局可用,但它对其他翻译单元不可见。我可以使用“static”关键字来指示对象的名称不被导出到链接器。

    假设我也有一个我只想在单个函数中使用的指针,但在函数调用之间保留了该指针的值。我可以再次使用“静态”关键字来表示对象具有静态范围;它的内存将在程序启动时分配,直到程序结束才释放,因此对象的值将在函数调用之间保留。

    /** 
     * foo.h
     */
    #ifndef FOO_H
    #define FOO_H
    
    /**
     * Non-defining declaration of aGlobalPointer; makes the name available
     * to other translation units, but does not allocate the pointer object
     * itself.
     */
    extern int *aGlobalPointer;
    
    /**
     * A function that uses all three pointers (extern on a function 
     * declaration serves roughly the same purpose as on a variable declaration)
     */
    extern void demoPointers(void);
    ...
    #endif
    
    /**
     * foo.c
     */
    #include "foo.h"
    
    /**
     * Defining declaration for aGlobalPointer.  Since the declaration 
     * appears at file scope (outside of any function) it will have static 
     * extent (memory for it will be allocated at program start and released 
     * at program end), and the name will be exported to the linker.
     */
    int *aGlobalPointer;
    
    /**
     * Defining declaration for aLocalPointer.  Like aGlobalPointer, it has 
     * static extent, but the presence of the "static" keyword prevents 
     * the name from being exported to the linker.  
     */
    static int *aLocalPointer;
    
    void demoPointers(void)
    {
      /**
       * The static keyword indicates that aReallyLocalPointer has static extent, 
       * so the memory for it will not be released when the function exits,
       * even though it is not accessible outside of this function definition
       * (at least not by name)
       */
      static int *aReallyLocalPointer;
    }
    

    【讨论】:

      【解决方案4】:

      How to correctly use the extern keyword in C

      还有Internal static variables in C, would you use them?

      本质上,“静态”(在标准 C 中)在函数中使用时允许变量不会像函数结束后通常那样被清除(即,每次调用函数时它都保留其旧值)。 “Extern”扩展了变量的范围,因此可以在其他文件中使用(即,它使其成为全局变量)。

      【讨论】:

        【解决方案5】:

        简短的回答。 静态是持久的,因此如果您在函数中声明它,当您再次调用该函数时,其值与上次相同。如果你全局声明它,它只在那个文件中是全局的。

        Extern 表示它是全局声明的,但在不同的文件中。 (这基本上意味着这个变量确实存在,这就是它的定义)。

        【讨论】:

          猜你喜欢
          • 2013-04-18
          • 1970-01-01
          • 1970-01-01
          • 2020-08-04
          • 2013-06-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-02-03
          相关资源
          最近更新 更多