473,405 Members | 2,445 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,405 software developers and data experts.

static variables & their initializations

vp
Can I safely assume that all static variables are initialized as NULL
or zero, depending on the types of the variables, no matter on which
platform that app is compiled ?

Thanks for your help,
DT
Nov 14 '05 #1
9 2467
dt*******@yahoo.com (vp) wrote in
news:24**************************@posting.google.c om:
Can I safely assume that all static variables are initialized as NULL
or zero, depending on the types of the variables, no matter on which
platform that app is compiled ?


If the platform has a conforming C compiler all static *non-initialized*
variables will be zero. Same for file scoped vars.

e.g.

int foo; /* will be zero at run-time */
int fob = 2; /* won't be zero */

int main(void)
{
static int baz; /* will be zero at run-time */
static int bar = 5; /* won't be zero */
return 0;
}

--
- Mark ->
--
Nov 14 '05 #2
"vp" <dt*******@yahoo.com> wrote in message
news:24**************************@posting.google.c om...
Can I safely assume that all static variables are initialized as NULL
or zero, depending on the types of the variables, no matter on which
platform that app is compiled ?


Almost. You can only assume that static variables without explicit
initialisors are so initialised.

e.g. static int x = 4; will not initialise x to zero.

Sounds obvious, but a more complicated example might be...

static int x;

int f(void)
{
return x;
}

static int x = 4;

Here, x will be initialised to 4 during program startup.

Note that aggregates without complete initialisers are default
initialised...

int array[4] = { 42 }; /* array 1..3 will be 0 */

--
Peter
Nov 14 '05 #3
On 29 Dec 2003 06:54:03 -0800, dt*******@yahoo.com (vp) wrote:
Can I safely assume that all static variables are initialized as NULL
or zero, depending on the types of the variables, no matter on which
platform that app is compiled ?
IIRC, the final C99 standard didn't change from the draft, and the draft says:

If an object that has static storage duration is not initialized explicitly,
then:
— if it has pointer type, it is initialized to a null pointer;
— if it has arithmetic type, it is initialized to (positive or unsigned) zero;
— if it is an aggregate, every member is initialized (recursively) according
to these rules;
— if it is a union, the first named member is initialized (recursively)
according to these rules.

If a compiler doesn't follow these rules, then it is in error. That's not to say
that all compilers follow the rules, though.
Thanks for your help,
DT


--
Lew Pitcher
IT Consultant, Enterprise Technology Solutions
Toronto Dominion Bank Financial Group

(Opinions expressed are my own, not my employers')
Nov 14 '05 #4
vp
Mark, Peter, Lew, thanks for all of your quick replies.

Yes, I am interested in the static variables that are not explicitly
initialized by the program.

Your replies let me know exactly what I want to be sure.

Thanks again,

DT
Nov 14 '05 #5
On 29 Dec 2003 15:20:12 GMT, "Mark A. Odell" <no****@embeddedfw.com>
wrote in comp.lang.c:
dt*******@yahoo.com (vp) wrote in
news:24**************************@posting.google.c om:
Can I safely assume that all static variables are initialized as NULL
or zero, depending on the types of the variables, no matter on which
platform that app is compiled ?
If the platform has a conforming C compiler all static *non-initialized*
variables will be zero. Same for file scoped vars.


Terminology problem here, all file scope variables have static storage
duration regardless of the use of the overloaded static keyword.
e.g.

int foo; /* will be zero at run-time */
This is a static int object. It has external linkage, whereas adding
the static keyword to the definition would change the linkage to
internal. But it is static (has static storage duration) either way.
int fob = 2; /* won't be zero */

int main(void)
{
static int baz; /* will be zero at run-time */
static int bar = 5; /* won't be zero */
return 0;
}

--
- Mark ->


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 14 '05 #6
Jack Klein <ja*******@spamcop.net> wrote in
news:0m********************************@4ax.com:
> Can I safely assume that all static variables are initialized as NULL
> or zero, depending on the types of the variables, no matter on which
> platform that app is compiled ?


If the platform has a conforming C compiler all static
*non-initialized* variables will be zero. Same for file scoped vars.


Terminology problem here, all file scope variables have static storage
duration regardless of the use of the overloaded static keyword.


True enough. In fact I just think of 'static' as always meaning reduced
scope and of static duration. Even though static on block scoped vars.
doesn't really reduce the scope of said var, it is a harmless lie for me.

--
- Mark ->
--
Nov 14 '05 #7
Mark A. Odell wrote:

Jack Klein <ja*******@spamcop.net> wrote in
news:0m********************************@4ax.com:
> Can I safely assume that all
> static variables are initialized as NULL
> or zero, depending on the types of the variables,
> no matter on which platform that app is compiled ?

If the platform has a conforming C compiler all static
*non-initialized* variables will be zero.
Same for file scoped vars.


Terminology problem here,
all file scope variables have static storage
duration regardless of the use of the overloaded static keyword.


True enough.
In fact I just think of 'static' as always meaning reduced
scope and of static duration. Even though static on block scoped vars.
doesn't really reduce the scope of said var,
it is a harmless lie for me.


I think of static locals,
as globals with reduced scope and internal linkage.
(global == external complete object)
The common points being static duration, and default initialization.

--
pete
Nov 14 '05 #8
On Mon, 29 Dec 2003 15:37:22 GMT
Le*********@td.com (Lew Pitcher) wrote:
On 29 Dec 2003 06:54:03 -0800, dt*******@yahoo.com (vp) wrote:
Can I safely assume that all static variables are initialized as NULL
or zero, depending on the types of the variables, no matter on which
platform that app is compiled ?


IIRC, the final C99 standard didn't change from the draft, and the
draft says:

If an object that has static storage duration is not initialized
explicitly, then:
_ if it has pointer type, it is initialized to a null pointer;
_ if it has arithmetic type, it is initialized to (positive or
unsigned) zero;_ if it is an aggregate, every member is initialized
(recursively) according
to these rules;
_ if it is a union, the first named member is initialized
(recursively)
according to these rules.

If a compiler doesn't follow these rules, then it is in error. That's
not to say that all compilers follow the rules, though.


IIRC (and I could be wrong) *one* C compiler I used for embedded work
was broken in that it only initialised static variables that were
explicitly initialised in the source code. However, this was easily
fixed by modifying the bootstrap code (the source of which was provided)
to initialise all RAM to zero (null pointers being all bits 0 in this
case) before doing other initialisation. This was back in 1995, so I
don't know if non-conforming compilers are still around.
--
Flash Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spam, it is real and I read it.
Nov 14 '05 #9
JV

"Flash Gordon" <sp**@flash-gordon.me.uk> wrote in message
news:20031230125718.5a79b5c4.sp**@flash-gordon.me.uk...
IIRC (and I could be wrong) *one* C compiler I used for embedded work
was broken in that it only initialised static variables that were
explicitly initialised in the source code. However, this was easily
fixed by modifying the bootstrap code (the source of which was provided)
to initialise all RAM to zero (null pointers being all bits 0 in this
case) before doing other initialisation. This was back in 1995, so I
don't know if non-conforming compilers are still around.
--
Flash Gordon
Paid to be a Geek & a Senior Software Developer
Although my email address says spam, it is real and I read it.


I've seen a similiar trouble in one compiler and I also work with embedded
systems.So it is not so common, but if can been pretty hard to figure out
when it happens, so I would still allways intialize the static variables if
it is required that they have a certain value when program starts.
-Jyrki
Nov 14 '05 #10

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

Similar topics

9
by: Tim Clacy | last post by:
Would some kind soul suggest a pre-processor test for the C++ language revision whereby class static variables were specified to refer to the same instance? Specifically, the following Singleton...
1
by: Harald Deischinger | last post by:
I am using a source file with the following style (I know it is not very beautiful but it is working): SRC1.cpp: static void vFoo() { // bla bla bla } static int iInit1(name, fun) { //...
1
by: cppaddict | last post by:
I would like to know the best way to initialize complex static member variables. In addition, I want to avoid creating an Init() method that is called by the ctor, since there's no need to wait...
115
by: Mark Shelor | last post by:
I've encountered a troublesome inconsistency in the C-language Perl extension I've written for CPAN (Digest::SHA). The problem involves the use of a static array within a performance-critical...
12
by: jyu.james | last post by:
I'm trying to detect reads of uninitialized global variables (that are declared in one file, and used in another as an extern). I know that ANSI C initializes all global variables to 0, however,...
5
by: Coder Coder | last post by:
Hello, Is it possible in C# to have the following. public class Foo { static { } public Foo ()
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...
20
by: JohnQ | last post by:
The way I understand the startup of a C++ program is: A.) The stuff that happens before the entry point. B.) The stuff that happens between the entry point and the calling of main(). C.)...
17
by: copx | last post by:
I don't know what to think of the following.. (from the dietlibc FAQ) Q: I see lots of uninitialized variables, like "static int foo;". What gives? A: "static" global variables are initialized...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...
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
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,...

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.