initialization of built-in types

A

Alexander Stippler

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
 
A

Alf P. Steinbach

* 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
 
G

Greg

Alexander said:
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
 
S

shailendra

It is basically depend on the type of variables, I mean the storage
type.

storage type | default value
 
A

Alf P. Steinbach

* 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.
 
R

Ron Natalie

Alexander said:
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.
 
G

Greg Comeau

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top