473,602 Members | 2,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2580
cs****@gmail.co m 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.c omwrote in message
news:11******** ************@z2 4g2000prh.googl egroups.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************ ********@z24g20 00prh.googlegro ups.com>
<cs****@gmail.c omwrote:
>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.co m 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.n etwrote:
<cs1...@gmail.c omwrote:
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*****@inspir e.net.nzwrites:
On Aug 24, 8:48 am, Chris Torek <nos...@torek.n etwrote:
><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_Keit h) 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.co m 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...@mindsp ring.comwrote:
cs1...@gmail.co m 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.c omwrote in message
news:11******** *************@r 23g2000prd.goog legroups.com...
On Aug 24, 2:32 am, pete <pfil...@mindsp ring.comwrote:
>cs1...@gmail.c om 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

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

Similar topics

2
2457
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 { print "Setting<BR>";
8
2529
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 static variables) is initializated (constructed) before main is invoked . . ." .. . .
8
4572
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: VarArray::funct were an extern, but it is declared in the same file (q.v.). What is the remedy for this? =================
3
1376
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 ..;} } // end SomeClass static SomeClass staticObject; // declaration in header -------------- in .cpp file: ----
53
26353
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 variable? My answer: static variable in function and global variable are allocated in head, and automatic variable is allocated in stack. Right?
6
3548
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 uncaught exception. How to catch an exception that happened before main() try { ... } catch (...) { ... } block? Is there a way?
55
6186
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 C# in some way? Or maybe no, because it is similar to a global variable (with its scope restricted) which C# is dead against? Zytan
14
2564
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 in severel 'underwater operations' * there is a list which stores objects of class B There are several issues I'm not sure about:
14
360
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 variable in one of the methods in the flow,
16
2040
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 frequently use this for such things as caching, or for keeping track of how many objects a factory function has created, and so on. Today it occurred to me to use a mutable object as the default value of a parameter. A simple example: def...
0
7920
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8401
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8404
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8054
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8268
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5867
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3900
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3944
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1254
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.