免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4887 | 回复: 9

[C] [讨论]关于C语言数据存储类型关键字static的问题 [复制链接]

论坛徽章:
0
发表于 2007-11-22 23:05 |显示全部楼层
5可用积分
在C语言数据存储类型关键字中有一个static,static有三种链接类型(linkage type),即static with no linkage, static with internal linkage 以及 static with external linkage。我在一本书上,即C Primer Plus  ,看到对static with no linkage的描述,如下:

    Static, no linkage— A variable declared in a block with the static storage class modifier belongs to the "static, no linkage" storage class. It has static storage duration, block scope, and no linkage. It is initialized just once, at compile time. If not initialized explicitly, its bytes are set to 0.

   译文:静态存储类型,无链接类型——在一个程序段中声明的变量,具有静态存储类型,其所属的存储类型为“静态存储类型,无链接类型”。它具有静态存储持续期,程序段作用域,无链接。它只可初始化一次,在编译时进行。如果没有经过明确初始化则具有该存储类型的变量所有字节都设置为0。

   按照这样的对“static, no linkage”的定义,则具有该存储类型的变量就只具有“block linkage”,即它只在一个程序段中起作用,言下之意就是在程序的其他地方不起作用。

   而稍后我又在该书的练习题中发现这样的一个问题:

   Which storage classes create variables that persist for the duration of the containing program?

   译文:何种存储类型能够产生出变量,该变量能够在其所处的程序范围内持续存在?

   给出的参考答案是:

     The static, no linkage storage class, the static, internal linkage storage class, and the static, external linkage storage class.
     译文:静态,无链接存储类型、静态,内部链接存储类型以及静态,外部链接类型
   
   按照这样的参考答案,也就是说static no linkage类型的变量不只可以在其所处的程序段范围内存在还可以持续在整个程序范围内存在了?这和前面提出的static, no linkage类型的定义不矛盾吗?而且我做了一个测试程序(如下)也证明定义是正确的而这个练习问题的参考答案不正确。

/* experimenting with static and auto */

#include <stdio.h>

int main(void){
   
    int a = 5;
    int b = 4;
   
    if(a > b){
        static int c = a;  //这里c = a 这个表达式也非法,c不能被赋予非常量值

        int d = b;
        printf("\nc = %d, d = %d, c - d = %d\n",
                 c,d,c-d);
    }
    //printf("\nc = %d, d = %d\n"c,d); 这里c、d已经不为编译器所识别了
    getch();
    return 0;
}   
谢谢!

论坛徽章:
0
发表于 2007-11-22 23:11 |显示全部楼层
It has static storage duration, block scope, and no linkage.

三者并列关系, static storage duration和block scope同时具备. 一个block里面的变量具有static storage duration.

论坛徽章:
0
发表于 2007-11-22 23:20 |显示全部楼层
书上说得没错的,代码块内以static限定的变量只是改变它的持续性而已,链接性和作用域不变,所以这样的变量作用域仅限于代码块内,但是生命期确是整个程序的。

论坛徽章:
0
发表于 2007-11-22 23:20 |显示全部楼层
原帖由 Edengundam 于 2007-11-22 23:11 发表
It has static storage duration, block scope, and no linkage.

三者并列关系, static storage duration和block scope同时具备. 一个block里面的变量具有static storage duration.

但是static with no linkage(block scope)似乎不能在整个程序期间持续存在吧,我的测试程序已经证明了这一点,但为什么练习问题答案中却说具有这种存储类型的变量却能在整个程序期间持续存在呢?

论坛徽章:
0
发表于 2007-11-22 23:21 |显示全部楼层
static变量表示其生存期是一直存在的,也表示它对外是不可见的,这不矛盾

   static int c = a;  //这里c = a 这个表达式也非法,c不能被赋予非常量值

c是在目标文件中存在的,给它一个a,不知道到底初始化成什么(还不知道a到底是什么)

论坛徽章:
0
发表于 2007-11-22 23:22 |显示全部楼层
不存在和不可见的区别

论坛徽章:
0
发表于 2007-11-22 23:22 |显示全部楼层
原帖由 mcmay 于 2007-11-22 23:20 发表

但是static with no linkage(block scope)似乎不能在整个程序期间持续存在吧,我的测试程序已经证明了这一点,但为什么练习问题答案中却说具有这种存储类型的变量却能在整个程序期间持续存在呢?


你没分清block scope, 先看block定义去.

如果你在读C语言引用手册, 建议先读K&R的CPL, 然后可以考虑直接看C99标准.

论坛徽章:
0
发表于 2007-11-22 23:23 |显示全部楼层
原帖由 zwylinux 于 2007-11-22 23:20 发表
书上说得没错的,代码块内以static限定的变量只是改变它的持续性而已,链接性和作用域不变,所以这样的变量作用域仅限于代码块内,但是生命期确是整个程序的。

“持续性”与“生命期”有何联系与区别?

论坛徽章:
0
发表于 2007-11-22 23:24 |显示全部楼层
原帖由 mcmay 于 2007-11-22 23:23 发表

“持续性”与“生命期”有何联系与区别?



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,25) and retains
its last-stored value throughout its lifetime.26) If an object is referred to outside of its
lifetime, the behavior is undefined. The value of a pointer becomes indeterminate when
the object it points to reaches the end of its lifetime.

论坛徽章:
0
发表于 2007-11-22 23:26 |显示全部楼层
谢谢各位指教,我要多去看看书才是!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP