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 9 2505 dt*******@yahoo .com (vp) wrote in
news:24******** *************** ***@posting.goo gle.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.
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 ->
--
"vp" <dt*******@yaho o.com> wrote in message
news:24******** *************** ***@posting.goo gle.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 ?
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
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')
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
On 29 Dec 2003 15:20:12 GMT, "Mark A. Odell" <no****@embedde dfw.com>
wrote in comp.lang.c: dt*******@yahoo .com (vp) wrote in news:24******** *************** ***@posting.goo gle.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.
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.l earn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jack Klein <ja*******@spam cop.net> wrote in
news:0m******** *************** *********@4ax.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.
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 ->
--
Mark A. Odell wrote: Jack Klein <ja*******@spam cop.net> wrote in news:0m******** *************** *********@4ax.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.
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
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.
"Flash Gordon" <sp**@flash-gordon.me.uk> wrote in message
news:2003123012 5718.5a79b5c4.s p**@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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 template will work with some
compilers but not with older ones (because every module that includes the
header gets its own unique static 'instance'):
template<typename T>
struct Singleton
{
static T& Instance() { static T instance; return...
|
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) {
// access some global variables to register fun as name.
|
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 for that
call in my application. My current attempt is OK, but it creates an
extra member variable that is extraneous:
<code>
class A {
private:
static std::vector<int> _v;
|
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 transform function.
When compiling under gcc on my big-endian PowerPC (Mac OS X),
declaring this array as "static" DECREASES the transform throughput by
around 5%. However, declaring it as "static" on gcc/Linux/Intel
INCREASES the throughput by...
|
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, I do
not want to rely on this for initialization. Instead, I want to
explicity initialize all variables myself. I've looked at tools like
Compuware BoundsChecker, which does an amazing job in detecting
uninitialized variables, but doesn't...
| |
by: Coder Coder |
last post by:
Hello,
Is it possible in C# to have the following.
public class Foo
{
static
{
}
public Foo ()
|
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?
|
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.) main().
So, if the above is OK, does static initialization occur during A or B? What
happens during A?
|
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 to 0. ANSI C guarantees that.
Technically speaking, static variables go into the .bss ELF segment,
while "static int foo=0" goes into .data. Because .bss is zero
filled by the OS, it does not need to be in the actual binary. So it
is in fact...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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,...
| |
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...
|
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,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |