473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

static globals

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-

Aug 23 '07 #1
11 2562
cs****@gmail.com wrote:
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 ??
Yes. Only const disallows modification.
Since static keywords makes globals to file specific during
compilation.
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.
Aug 23 '07 #2
<cs****@gmail.comwrote in message
news:11********************@z24g2000prh.googlegrou ps.com...
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.
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).
Aug 23 '07 #3
In article <11********************@z24g2000prh.googlegroups.c om>
<cs****@gmail.comwrote:
>I wanted to [change] a global variable
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}};

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,
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.
Aug 23 '07 #4
On Aug 24, 12:57 am, cs1...@gmail.com wrote:
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.
'static' means that the identifier 'rowCols' is not visible
outside of that file. However, the memory itself can still
be modified by other parts of that program, it's just that
the other part of the program cannot look up the name 'rowCols'.

Aug 23 '07 #5
On Aug 24, 8:48 am, Chris Torek <nos...@torek.netwrote:
<cs1...@gmail.comwrote:
I am updating this global variable in one of the methods in the flow,

If you are using a C++ "method", you have left C behind,
C++ does not have methods any more than C does.
Class member functions are called 'member functions'.

People seem to call functions of either type 'methods'.

Aug 23 '07 #6
Old Wolf <ol*****@inspire.net.nzwrites:
On Aug 24, 8:48 am, Chris Torek <nos...@torek.netwrote:
><cs1...@gmail.comwrote:
>I am updating this global variable in one of the methods in the flow,

If you are using a C++ "method", you have left C behind,

C++ does not have methods any more than C does.
Class member functions are called 'member functions'.

People seem to call functions of either type 'methods'.
<OT>
The C++ standard uses the word "method" only in its normal English
sense, but Stroustrups book _The C++ Programming Language_ says

A virtual member function is sometimes called a _method_.

so it's probably not entriely unreasonable to talk about "methods" in
C++.
</OT>

I marked the above OT because I'm reading and posting in comp.lang.c.
If you want to discuss whether C++ has "methods", please drop the
comp.lang.c cross-post. Thanks.

But C certainly doesn't have "methods" in the sense used by some
object-oriented languages (you can implement OO in C, and you might
talk about OO methods in that context, but that's unusual).

I'm more confused about what the OP meant by "in the flow".

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 23 '07 #7
cs****@gmail.com wrote:
>
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}};
As far as I can tell from your post,
you don't know why you want to make your global static.

Why do you want to make your global static?

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.
--
pete
Aug 24 '07 #8
On Aug 24, 2:32 am, pete <pfil...@mindspring.comwrote:
cs1...@gmail.com wrote:
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}};

As far as I can tell from your post,
you don't know why you want to make your global static.

Why do you want to make your global static?
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.

--
pete

Hi Pete,
I get a linker warning LNK4006 with MicroSoft CL compiler, whic is
"symbol already defined in object; second definition ignored"

Wanted to remove this warning from compilation.

regard,
chandra-

Aug 24 '07 #9
<cs****@gmail.comwrote in message
news:11*********************@r23g2000prd.googlegro ups.com...
On Aug 24, 2:32 am, pete <pfil...@mindspring.comwrote:
>cs1...@gmail.com wrote:
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}};

As far as I can tell from your post,
you don't know why you want to make your global static.

Why do you want to make your global static?
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.

--
pete


Hi Pete,
I get a linker warning LNK4006 with MicroSoft CL compiler, whic is
"symbol already defined in object; second definition ignored"

Wanted to remove this warning from compilation.
If you want to change this variable in a function/method and you use the
static keyword, then you will have two different instances of the variable,
with different values. Probably not what you want.

So, how to get it so that both object files point to the same variable and
don't give you a compilation warning error? That is where extern comes in.

In your header file you would use:

extern double rowCols[3][3];

Having this in more that one compilation unit is fine. In fact, having it
multiple times in the same compilation unit should produce no
warnings/errors.

Then, in a .cpp file (pick one) you would do:

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

Now, there is only one instance of rowCols, but every compilation unit that
includes the header with the extern will point to the same one.

extern means, basically, I'm going to to have a variable named rowCols[3][3]
but I"m not defining it here, it's defined in another compilation unit, when
you link, link to the same one.

Now, when a method/function changes the value of the variables, since there
is only one instance of the variable, all compilation units will "see" the
change.
Aug 24 '07 #10
cs****@gmail.com wrote:
I get a linker warning LNK4006 with MicroSoft CL compiler, whic is
"symbol already defined in object; second definition ignored"

Wanted to remove this warning from compilation.
That warning is trivially removed by, surprise, _not defining the symbol
twice_. Not by buggering about with globals and statics.

Richard
Aug 24 '07 #11
On Fri, 24 Aug 2007 01:47:30 -0700, cs****@gmail.com wrote:
>On Aug 24, 2:32 am, pete <pfil...@mindspring.comwrote:
snip
>As far as I can tell from your post,
you don't know why you want to make your global static.

Why do you want to make your global static?
snip
>I get a linker warning LNK4006 with MicroSoft CL compiler, whic is
"symbol already defined in object; second definition ignored"

Wanted to remove this warning from compilation.
You have, at the very least, a coding error in your program. It could
a design error, which is usually worse. While changing the object to
static may eliminate the diagnostic, it does nothing for the
underlying problem. All you have done is masked a symptom.
Remove del for email
Aug 28 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ryan Hubbard | last post by:
Could someone provide me with some code to hold an object reference in a static variable in a function. function a(&$t){ static $b; if(is_object($b)){ print "IN - $b->logfile"; } else {...
8
by: jose luis fernandez diaz | last post by:
Hi, I am reading Stroustrup's book 'C++ Programming Language'. In the 10.4.9 section (Nonlocal Store) he says: "A variable defined outside any function (that is global, namespace, and class...
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
3
by: noleander | last post by:
Im getting a runtime error because Ive got a static object that is not properly initialized. ---------- header file xxx.hpp --------------- class SomeClass { SomeClass() { .. constructor here...
53
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global...
6
by: Marvin Barley | last post by:
I have a class that throws exceptions in new initializer, and a static array of objects of this type. When something is wrong in initialization, CGI program crashes miserably. Debugging shows...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
14
by: Jeroen | last post by:
Hi all, I've got a question about writing a library. Let me characterize that library by the following: * there is a class A which is available to the user * there is a class B that is used...
14
by: cs1975 | last post by:
Hi Everyone, I wanted to make a global variable double rowCols = {{1,0,0},{0,1,0},{1,0,0}}; to static global static double rowCols = {{1,0,0},{0,1,0},{1,0,0}}; I am updating this global...
16
by: Joe Strout | last post by:
One thing I miss as I move from REALbasic to Python is the ability to have static storage within a method -- i.e. storage that is persistent between calls, but not visible outside the method. I...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.