By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,719 Members | 1,046 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,719 IT Pros & Developers. It's quick & easy.

initialization of built-in types

P: n/a
Hello,

I have some class members of built-in type (to be exact: int) and wonder
about the initialization behaviour of gcc4.0.
I thought, these members would be implicitly be initialized to zero - if
not assigned a different value explicitely in the constructor, but I
noticed that they were uninitialized. What does the standard say in this
context?

regards,
Alex
Oct 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
* Alexander Stippler:

I have some class members of built-in type (to be exact: int) and wonder
about the initialization behaviour of gcc4.0.
I thought, these members would be implicitly be initialized to zero - if
not assigned a different value explicitely in the constructor, but I
noticed that they were uninitialized. What does the standard say in this
context?


uninitialized

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 17 '05 #2

P: n/a

Alexander Stippler wrote:
Hello,

I have some class members of built-in type (to be exact: int) and wonder
about the initialization behaviour of gcc4.0.
I thought, these members would be implicitly be initialized to zero - if
not assigned a different value explicitely in the constructor, but I
noticed that they were uninitialized. What does the standard say in this
context?

regards,
Alex


If the object is global or static it will be zero-initialized by
default.

Greg

Oct 17 '05 #3

P: n/a
It is basically depend on the type of variables, I mean the storage
type.

storage type | default value
------------------------------------------------
auto | garbage
register | I dont know, find out
static | zero
extern | no default value as it just a declaration

Oct 17 '05 #4

P: n/a
* shailendra:
It is basically depend on the type of variables, I mean the storage
type.

storage type | default value
------------------------------------------------
auto | garbage
register | I dont know, find out
static | zero
extern | no default value as it just a declaration


Please quote what you're replying to (I know it's difficult with Google, but
there are some places to click that will let you insert quoted material).

Well yes, it depends just as you write. In addition, don't forget dynamically
allocated objects: there it's also uninitialized by default (or more precisely
there's no initialization guarantee if you don't specify that you want
initialization). And it might perhaps help to note that there are no special
"storage type" rules, no table to memorize, except there is a rule for static
storage duration where any object, even an object with constructor, is
zero-initialized, which happens before any other initialization such as
constructor execution.

But since you're replying to my reply, that reply was an answer to Alexander's
question, quote, "in this context", where he got garbage.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 17 '05 #5

P: n/a

Greg wrote:
Alexander Stippler wrote:
Hello,

I have some class members of built-in type (to be exact: int) and wonder
about the initialization behaviour of gcc4.0.
I thought, these members would be implicitly be initialized to zero - if
not assigned a different value explicitely in the constructor, but I
noticed that they were uninitialized. What does the standard say in this
context?

regards,
Alex


If the object is global or static it will be zero-initialized by
default.

Greg


Where does it state that in the standard?

Oct 17 '05 #6

P: n/a
On 17 Oct 2005 10:13:06 -0700, "Axter" <go****@axter.com> wrote:
If the object is global or static it will be zero-initialized by
default.

Greg


Where does it state that in the standard?


See section 3.6.2, paragraph 1 as well as section 9.4.2, paragraph 7.

--
Bob Hairgrove
No**********@Home.com
Oct 17 '05 #7

P: n/a
Alexander Stippler wrote:
Hello,

I have some class members of built-in type (to be exact: int) and wonder
about the initialization behaviour of gcc4.0.
I thought, these members would be implicitly be initialized to zero - if
not assigned a different value explicitely in the constructor, but I
noticed that they were uninitialized. What does the standard say in this
context?

While the default initialization of POD types is zero initialization,
the stupid-bug-compatibility with C says that under certain
circumstances we neglect to bother to default initialize objects.
This is one of them.
Oct 17 '05 #8

P: n/a
In article <ha********************************@4ax.com>,
Bob Hairgrove <in*****@bigfoot.com> wrote:
On 17 Oct 2005 10:13:06 -0700, "Axter" <go****@axter.com> wrote:
If the object is global or static it will be zero-initialized by
default.

Greg


Where does it state that in the standard?


See section 3.6.2, paragraph 1 as well as section 9.4.2, paragraph 7.


Noting that some of those [globals and] statics may also go
through additional initialization after being zero'd.
--
Greg Comeau / Celebrating 20 years of Comeauity!
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Oct 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.