静态全局变量 [英] static globals

查看:86
本文介绍了静态全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我想创建一个全局变量

double rowCols [3] [3] = {{1,0,0} ,{0,1,0},{1,0,0}};


到静态全局


静态双rowCols [3 ] [3] = {{1,0,0},{0,1,0},{1,0,0}};


我正在更新此全局变量流程中的一种方法,


将此变量改为

静态后真的可以改变吗?


由于静态关键字使得全局变量在

编译期间具有特定的文件。


关于此问题的任何帮助都会有所帮助。


热烈的问候,

Chandra-

Hi Everyone,

I wanted to make a global variable
double rowCols[3][3] = {{1,0,0},{0,1,0},{1,0,0}};

to static global

static double rowCols[3][3] = {{1,0,0},{0,1,0},{1,0,0}};

I am updating this global variable in one of the methods in the flow,

Is it really possible to change after making this variable to
static ??

Since static keywords makes globals to file specific during
compilation.

Any help regarding this issue will be helpful.

Warm regards,
Chandra-

推荐答案

cs **** @ gmail.com 写道:
cs****@gmail.com wrote:

大家好,


我想创建一个全局变量

double rowCols [3] [3] = {{1,0,0},{0,1,0},{ 1,0,0}};


到静态全局


static double rowCols [3] [3] = {{1,0,0},{0,1,0},{1,0,0}};


我是在流程中的一个方法中更新这个全局变量,


将这个变量改为

static后真的可以改变吗?
Hi Everyone,

I wanted to make a global variable
double rowCols[3][3] = {{1,0,0},{0,1,0},{1,0,0}};

to static global

static double rowCols[3][3] = {{1,0,0},{0,1,0},{1,0,0}};

I am updating this global variable in one of the methods in the flow,

Is it really possible to change after making this variable to
static ??



是的。只有const不允许修改。

Yes. Only const disallows modification.


由于静态关键字使得全局变量在

编译期间变为特定文件。
Since static keywords makes globals to file specific during
compilation.



如果你需要从另一个文件访问数组,不要用

static声明它。在一个文件中将其定义为一个不合格的全局,并在包含extern声明后从其他文件中访问它。

If you need to access the array from another file, don''t declare it with
static. Define it as an unqualified global in exactly one file and access
it from other files after including an extern declaration.


< cs ** **@gmail.com在留言中写道

新闻:11 ******************** @ z24g2000prh.googlegrou ps.com ...
<cs****@gmail.comwrote in message
news:11********************@z24g2000prh.googlegrou ps.com...

大家好,


我想制作一个全局变量

double rowCols [3] [3] = {{1,0,0},{0,1,0},{1,0,0}};


到静态全局


静态双rowCols [3] [3] = {{1,0,0},{0,1,0},{1,0,0}};

我正在流程中的一个方法中更新这个全局变量,


将此变量改为
$ b $后是否真的可以更改b静态??


由于静态关键字使得全局变量在

编译期间特定于文件。


任何有关的帮助这个问题会有所帮助。
Hi Everyone,

I wanted to make a global variable
double rowCols[3][3] = {{1,0,0},{0,1,0},{1,0,0}};

to static global

static double rowCols[3][3] = {{1,0,0},{0,1,0},{1,0,0}};

I am updating this global variable in one of the methods in the flow,

Is it really possible to change after making this variable to
static ??

Since static keywords makes globals to file specific during
compilation.

Any help regarding this issue will be helpful.



应用于变量的静态具有不同的含义,具体取决于它使用的是什么



静态应用于全局变量表明该变量是

翻译单元的本地变量(它还有内部链接)。


基本上这意味着这个全局变量只会在这个

翻译单元(目标文件)中使用,而其他目标文件则不需要。它没有说明变量的恒定性。所以,是的,你可以在翻译单元中改变
更改rowCols。我相信这个

方法中使用的静态主要用于防止名称冲突。通常的做法是将#define更改为变量以将其声明为

static const


如果没有const,然而,它是该翻译单元的全局变量

可以像任何其他变量一样进行修改。


我没有尝试过它,但我会认为它可以用于

函数声明,以避免名称分类,如果你有两个不同的翻译单位声明相同的

函数(不要引用我在

,我可能错了)。

static applied to a variable has different meanings depending on where it''s
used.

static applied to a global variable states that the variable is local to the
translation unit (it also has internal linkage).

Basically that means that this global variable will only be used in this
translation unit (object file) and not needed by other object files. It
says nothing about the constantness of the variable. So, yes, you could
change rowCols in the translation unit. I believe static used in this
method is used primarily to prevent name collisions. It is common practice
when changing a #define to a variable to declare it as
static const

Without the const, however, it is a global variable to that translation unit
that can be modified like any other variable.

I have not experimented with it, but I would think it could be used for a
function declaration as well to avoid name colisions if you have the same
function declared in two different translation units (don''t quote me on
that, I may be wrong).


在文章< 11 ************* *******@z24g2000prh.googlegroups.c om>

< cs **** @ gmail.comwrote:
In article <11********************@z24g2000prh.googlegroups.c om>
<cs****@gmail.comwrote:

>我想[更改]全局变量
>I wanted to [change] a global variable



短语global variable C标准没有定义(和/或
我认为不是C ++标准),而是指不同的东西

给不同的人。所以你回答问题的任何答案

答案 - 你认为这里的问题可能不是你想的问题的答案。*你认为你问过。

The phrase "global variable" is not defined by the C standard (and
I think not by the C++ standard either), and "means" different things
to different people. So any answer you get to the question the
answer-er thought you asked here may not be the answer to the
question *you* thought you asked.


> double rowCols [3] [3] = {{1,0,0},{0,1,0},{1,0,0}};

静态全局

static double rowCols [3] [3] = {{1,0,0},{0,1,0},{1,0 ,0}};

我正在流程中的一个方法中更新这个全局变量,
>double rowCols[3][3] = {{1,0,0},{0,1,0},{1,0,0}};

to static global

static double rowCols[3][3] = {{1,0,0},{0,1,0},{1,0,0}};

I am updating this global variable in one of the methods in the flow,



如果你使用的是C ++ 方法,你已经把C留下了,应该将你自己限制在comp.lang.c ++中。范围规则在C ++中略微不同于在C中,并且因为全局而在可以参考

到范围(而不是链接),你在comp.lang.c中得到的任何答案

可能不是你认为你问过的问题的答案。 br $> b $ b -

In-Real-Life:风河系统公司Chris Torek

美国犹他州盐湖城(40°39.22'' N,111°50.29''W)+1 801 277 2603

电子邮件:忘了它 http://web.torek.net/torek/index.html

阅读电子邮件就像在垃圾中搜索食物一样,感谢垃圾邮件发送者。

If you are using a C++ "method", you have left C behind, and should
restrict yourself to comp.lang.c++. The rules for scopes are
subtly different in C++ than in C, and since "global" may refer
to scope (rather than linkage), any answer you get in comp.lang.c
may not be the answer to the question you thought you asked.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22''N, 111°50.29''W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.


这篇关于静态全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆