473,473 Members | 1,949 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

zero initialization.

Hi all,

I recently just stumbled upon this code. According to what
I see in the standard it should be fine, but I've always done
the initialization explicitly myself:

//.h
//...
struct X
{
static Y* m_Y;
};

//.cpp
//To initialise...
Y* X::m_Y; //...1
//as opposed to...
Y* X::m_Y = NULL; //...2

I now know (1) is correct as well (from various areas in the
standard).
I personally prefer (2) - probably because I've always done it that
way (I think I might have even had a problem with (1) a couple
of years back).

Any thoughts on this? Do I understand the standard correctly if I
say (1) is OK?

My rationale from std98:
- 8.5:5 - to zero initialize...
- 3.6.2 - storage for objects with static storage...

Which method do you consider preferred, or is it a matter of taste?

Regards,

Werner
Dec 19 '07 #1
10 2411
On Dec 19, 10:16 pm, werasm <wer...@gmail.comwrote:
Hi all,

I recently just stumbled upon this code. According to what
I see in the standard it should be fine, but I've always done
the initialization explicitly myself:

//.h
//...
struct X
{
static Y* m_Y;

};

//.cpp
//To initialise...
Y* X::m_Y; //...1
I think static variables stored in the data segment bss, are always
initialized with 0...
just like global variables...
//as opposed to...
Y* X::m_Y = NULL; //...2

I now know (1) is correct as well (from various areas in the
Dec 19 '07 #2
Rahul wrote:
On Dec 19, 10:16 pm, werasm <wer...@gmail.comwrote:
>Hi all,

I recently just stumbled upon this code. According to what
I see in the standard it should be fine, but I've always done
the initialization explicitly myself:

//.h
//...
struct X
{
static Y* m_Y;

};

//.cpp
//To initialise...
Y* X::m_Y; //...1

I think static variables stored in the data segment bss, are always
initialized with 0...
To clarify...

It is unspecified what "segment" they are stored in, and there is no
reference to "bss" in the C++ language standard. Objects with static
storage duration have their memory zero-initialised before any other
dynamic initialisation takes place. If no other initialisation is
expected, the zero-filled memory is it.
just like global variables...
Global variables (declared at the namespace level) all have static
storage duration (if they have storage at all, that is).
>//as opposed to...
Y* X::m_Y = NULL; //...2

I now know (1) is correct as well (from various areas in the
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 19 '07 #3
jg
On Dec 19, 9:16 am, werasm <wer...@gmail.comwrote:
>
Which method do you consider preferred, or is it a matter of taste?
I think that (2) is preferred. A 'static member' means that all
objects
of X shares the same member. The 'static' here does not require
that 'my_Y' be in static storage, although it is in fact true in many
implementations.

JG
Dec 19 '07 #4
jg wrote:
On Dec 19, 9:16 am, werasm <wer...@gmail.comwrote:
>>
Which method do you consider preferred, or is it a matter of taste?
I think that (2) is preferred. A 'static member' means that all
objects
of X shares the same member. The 'static' here does not require
that 'my_Y' be in static storage, although it is in fact true in many
implementations.
I am not sure what "be in static storage" means here. Static class
data members are said to have static storage duration (see Standard,
[basic.stc.static]/4). Does that mean that they are "in static
storage"? Probably. If their storage has duration that is static,
then you can call their storage "static", and since those objects
are _in_ their storage, then it's OK to say that they are "in static
storage".

So, to argue, the Standard clearly _requires_ that static data
members "be in static storage". It has to be true for all compiler
implementations, otherwise they are not conforming.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 19 '07 #5
jg
On Dec 19, 11:06 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>
I think that (2) is preferred. A 'static member' means that all
objects
of X shares the same member. The 'static' here does not require
that 'my_Y' be in static storage, although it is in fact true in many
implementations.

I am not sure what "be in static storage" means here. Static class
data members are said to have static storage duration (see Standard,
[basic.stc.static]/4). Does that mean that they are "in static
storage"? Probably. If their storage has duration that is static,
Yes, this was what I meant.
then you can call their storage "static", and since those objects
are _in_ their storage, then it's OK to say that they are "in static
storage".

So, to argue, the Standard clearly _requires_ that static data
members "be in static storage".
You're probably right. The 9.4.2.6 says it has external linkage.
Does it mean it is definitely a static/global, so it shall be
automatically zero-initialized ?

JG
Dec 19 '07 #6
jg
On Dec 19, 12:10 pm, jg <jgu...@gmail.comwrote:
>
You're probably right. The 9.4.2.6 says it has external linkage.
Does it mean it is definitely a static/global, so it shall be
automatically zero-initialized ?
I found the answer for myself, 3.7.1(4) states a static member has
static storage duration.

Back to the original question, I think it is better to use
explicit initialization.

JG

Dec 19 '07 #7
werasm <we****@gmail.comwrote:
I recently just stumbled upon this code. According to what
I see in the standard it should be fine, but I've always done
the initialization explicitly myself:

//.h
//...
struct X
{
static Y* m_Y;
};

//.cpp
//To initialise...
Y* X::m_Y; //...1
//as opposed to...
Y* X::m_Y = NULL; //...2

I now know (1) is correct as well (from various areas in the
standard).
I personally prefer (2) - probably because I've always done it that
way (I think I might have even had a problem with (1) a couple
of years back).

Any thoughts on this? Do I understand the standard correctly if I
say (1) is OK?

My rationale from std98:
- 8.5:5 - to zero initialize...
- 3.6.2 - storage for objects with static storage...

Which method do you consider preferred, or is it a matter of taste?
Guru's generally recommend making things explicit in code rather than
leaving them implicit, so your (2) would be the preferred thing to do.
Dec 19 '07 #8
On 2007-12-19 16:22:44 -0500, "Daniel T." <da******@earthlink.netsaid:
werasm <we****@gmail.comwrote:
>I recently just stumbled upon this code. According to what
I see in the standard it should be fine, but I've always done
the initialization explicitly myself:

//.h
//...
struct X
{
static Y* m_Y;
};

//.cpp
//To initialise...
Y* X::m_Y; //...1
//as opposed to...
Y* X::m_Y = NULL; //...2

I now know (1) is correct as well (from various areas in the
standard).
I personally prefer (2) - probably because I've always done it that
way (I think I might have even had a problem with (1) a couple
of years back).

Any thoughts on this? Do I understand the standard correctly if I
say (1) is OK?

My rationale from std98:
- 8.5:5 - to zero initialize...
- 3.6.2 - storage for objects with static storage...

Which method do you consider preferred, or is it a matter of taste?

Guru's generally recommend making things explicit in code rather than
leaving them implicit, so your (2) would be the preferred thing to do.
Yes, there are "gurus" who don't believe that programmers ought to be
held to professional standards. So they tell us you shouldn't leave
static data implicitly initialized, because someone reading your code
might not know what that means, or, worse, might know what it means,
but think that you might not know what it means, and that you only got
the right result by accident.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Dec 19 '07 #9
On Dec 19, 11:04 pm, Pete Becker <p...@versatilecoding.comwrote:
On 2007-12-19 16:22:44 -0500, "Daniel T." <danie...@earthlink.netsaid:
werasm <wer...@gmail.comwrote:
I recently just stumbled upon this code. According to what
I see in the standard it should be fine, but I've always done
the initialization explicitly myself:
//.h
//...
struct X
{
static Y* m_Y;
};
//.cpp
//To initialise...
Y* X::m_Y; //...1
//as opposed to...
Y* X::m_Y = NULL; //...2
I now know (1) is correct as well (from various areas in the
standard).
I personally prefer (2) - probably because I've always done it that
way (I think I might have even had a problem with (1) a couple
of years back).
Any thoughts on this? Do I understand the standard correctly if I
say (1) is OK?
My rationale from std98:
- 8.5:5 - to zero initialize...
- 3.6.2 - storage for objects with static storage...
Which method do you consider preferred, or is it a matter of taste?
Guru's generally recommend making things explicit in code rather than
leaving them implicit, so your (2) would be the preferred thing to do.

Yes, there are "gurus" who don't believe that programmers ought to be
held to professional standards. So they tell us you shouldn't leave
static data implicitly initialized, because someone reading your code
might not know what that means, or, worse, might know what it means,
but think that you might not know what it means, and that you only got
the right result by accident.
Hmmm, does this mean you also suggest the latter?

Regards,

Werner
Dec 19 '07 #10
Pete Becker <pe**@versatilecoding.comwrote:
"Daniel T." <da******@earthlink.netsaid:
Guru's generally recommend making things explicit in code rather
than leaving them implicit.

Yes, there are "gurus" who don't believe that programmers ought to
be held to professional standards. So they tell us you shouldn't
leave static data implicitly initialized, because someone reading
your code might not know what that means, or, worse, might know
what it means, but think that you might not know what it means, and
that you only got the right result by accident.
Much like the common recommendation that one should never put leading
underscores on variable names simply because in some contexts such a
variable is reserved.

The less context dependent a rule is, the easier it is to remember. "int
foo = 0;" makes 'foo' equal 0 no matter the context, for "int foo;",
'foo' will equal 0 in some contexts and be undefined in others.

But I take your point that some Guru's tend to talk down to people.
Dec 20 '07 #11

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

Similar topics

6
by: Andrey | last post by:
What does the standard say about zero-initializing of static structures/classes, specifically: is it guaranteed by the standard that the alignment fillers between the members will also be...
4
by: Steven T. Hatton | last post by:
I mistakenly set this to the comp.std.c++ a few days back. I don't believe it passed the moderator's veto - and I did not expect or desire anything different. But the question remains: ISO/IEC...
9
by: Niels Dekker - no reply address | last post by:
Are all the following initializations semantically equivalent? wchar_t a = {L'\0'}; wchar_t b = {'\0'}; wchar_t c = {0}; wchar_t d = {}; If so, why don't we all use an empty initializer...
13
by: simondex | last post by:
Hi, Everyone! Does anyone know how to initialize an int array with a non-zero number? Thank You Very Much. Truly Yours, Simon Dexter
14
by: Stef | last post by:
Hello, I have a question: Is it possible to init a 2d array of structures to zero ? For example with array I do. int array = {0} but:
37
by: Ajai Jose | last post by:
Hi , I work on an ARM processor based embedded system. Code is mainly in C language. The project has a huge source base. We are supposed to optimise it. Most datastructures are declared as static...
23
by: anon.asdf | last post by:
Hello! In the following code-snippet, is it possible to initialize each element of arr, with STRUCT_INIT? struct mystruct { int a; char b; };
11
by: subramanian100in | last post by:
Suppose we have a class named Test. Test obj; // assuming default ctor is available Test direct_init(obj); // direct initialization happens here Test copy_init = obj; // copy initialization...
5
by: simmol | last post by:
Hi, I'm running a simulation over time and I'm having trouble with arrays in my C program. An array element that has been initialized to zero suddenly changes value to 13 after a few time points,...
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
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
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
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.