473,756 Members | 6,970 Online
Bytes | Software Development & Data Engineering Community
+ 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 2436
On Dec 19, 10:16 pm, werasm <wer...@gmail.c omwrote:
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.c omwrote:
>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.c omwrote:
>
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.c omwrote:
>>
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.stati c]/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...@com Acast.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.stati c]/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.c omwrote:
>
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.c omwrote:
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******@earth link.netsaid:
werasm <we****@gmail.c omwrote:
>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...@versatile coding.comwrote :
On 2007-12-19 16:22:44 -0500, "Daniel T." <danie...@earth link.netsaid:
werasm <wer...@gmail.c omwrote:
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

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

Similar topics

6
1659
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 initialized to zero? Andrey
4
4739
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 14882:2003(E) §8.5 says:   To zero-initialize an object of type T means: 5   -- if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;
9
27083
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 list, {}, when zero-initializing a C-style string? I was wondering, because the book C++ Coding Standards (Sutter & Alexandrescu) says at item 19, "Always
13
27155
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
2350
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
2336
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 and which directly go into the Zero Initialised region. We need to cut the size of this ZI region by at least 30%. The one way i see of doing this is by removing these static arrays and passing a pointer to the data structure whenever required....
23
3917
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
2408
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 happens here Suppose we have a function void fn(Test arg);
5
1667
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, and surprisingly, this only happens in some runs. If I print out values from different parts of the program, I can clearly see the value changing. I am not using dynamic memory allocation, so I'm not sure why I see this strange behavior. Has...
0
9487
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, 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...
0
9297
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
9904
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
9884
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
9735
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...
0
8736
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, 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...
1
7285
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...
1
3828
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
2
3395
muto222
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.