473,473 Members | 2,034 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

gcc and Static Variables

I am currently working on changing the compiler from Sun Workshop to
g++ for a mid sized C program. In the C files we have static variables
defined and declared. This works fine with the Sun Workshop compiler.
However under g++ the static variables are initialized to zero but do
not have the value they are assigned to. For example say file2.cc I
have static variable defined and declared as
static int j = 5;
Variable j seems to have a value of zero and not 5. However any static
variables defined and declared in the file that contains function main
seems to have the value that is assigned to them. I am using g++
version 3.2.2 in a Solaris 8 environment.
Any help would be appreciated.

Joe

Nov 14 '05 #1
3 4991
fe********@gmail.com wrote:
I am currently working on changing the compiler from Sun Workshop to
g++ for a mid sized C program. In the C files we have static variables
defined and declared. This works fine with the Sun Workshop compiler.
However under g++ the static variables are initialized to zero but do
not have the value they are assigned to. For example say file2.cc I
have static variable defined and declared as
static int j = 5;
File-scope static, or block-scope static?
Variable j seems to have a value of zero and not 5. However any static
variables defined and declared in the file that contains function main
seems to have the value that is assigned to them.


Since the C compile works, and the C++ one does not, this seems to me to
be a problem of C++, not of C, and to belong in comp.lang.c++, not here.
In any case, the line as you quote it should indeed result in the object
being initialised to 5; as far as this group is concerned, it is
correct.

Richard
Nov 14 '05 #2
fe********@gmail.com wrote:

I am currently working on changing the compiler from Sun Workshop
to g++ for a mid sized C program. In the C files we have static
variables defined and declared. This works fine with the Sun
Workshop compiler. However under g++ the static variables are
initialized to zero but do not have the value they are assigned
to. For example say file2.cc I have static variable defined and
declared as
static int j = 5;
Variable j seems to have a value of zero and not 5. However any
static variables defined and declared in the file that contains
function main seems to have the value that is assigned to them.
I am using g++ version 3.2.2 in a Solaris 8 environment.
Any help would be appreciated.


Sounds like something else wrong. For example, you should be using
gcc for C programs, because g++ primarily alters the default
libraries used. g++ is for C++, not C. At the same time recognize
how gcc identifies the language, i.e. (unless specifically told
otherwise) by the source extension. .C or .cpp identifies C++,
while .c identifies C.

At any rate this is off-topic for c.l.c, as is anything that is
system or compiler specific. You should look for a newsgroup
dealing with gnu and/or gcc.

Since you are using the broken google groups system, note the info
in my sig below.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #3
fe********@gmail.com wrote:
I am currently working on changing the compiler from Sun Workshop to
g++ for a mid sized C program.
g++ is a C++ compiler, not a C compiler. gcc is the C compiler.
In the C files we have static variables
defined and declared. This works fine with the Sun Workshop compiler.
However under g++ the static variables are initialized to zero but do
not have the value they are assigned to. For example say file2.cc I
Files ending in .cc are normally C++ source files, not C source files.
Although this convention is not mandated by the standard. This news
group only does C, not C++ which is covered just down the hall in
comp.lang.c++
have static variable defined and declared as
static int j = 5;
Variable j seems to have a value of zero and not 5. However any static
variables defined and declared in the file that contains function main
seems to have the value that is assigned to them. I am using g++
version 3.2.2 in a Solaris 8 environment.
Any help would be appreciated.


First decide if it is a C or C++ you are working with, then select the
appropriate group and compiler. Then provide to the appropriate group a
minimum compilable program exhibiting your problem and see what people say.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #4

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

Similar topics

1
by: James | last post by:
Hello Java NG, I not sure if this is the right NG for this type of question but if not please let me know which is, TIA Any way first off let me say I'm a student and this WAS last weeks lab,...
2
by: katekukku | last post by:
HI, Could anyone please tell me what are static variables and what exactly are there features. I am a little bit confused. Thank You
9
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static...
4
by: Dave | last post by:
I used the following class and .aspx code below to understand how static works on variables and methods taken from...
8
by: Simone Chiaretta | last post by:
I've a very strange behaveour related to a website we built: from times to times, something should happen on the server, and all static variables inside the web application, both defined inside aspx...
28
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. ...
5
by: Jesper Schmidt | last post by:
When does CLR performs initialization of static variables in a class library? (1) when the class library is loaded (2) when a static variable is first referenced (3) when... It seems that...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
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...
16
by: RB | last post by:
Hi clever people :-) I've noticed a lot of people stating not to use static variables with ASP.NET, and, as I understand it, the reason is because the variable is shared across user sessions -...
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
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,...
0
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...
1
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,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.