Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   class member "static array of pointers" NULL initialization (http://www.velocityreviews.com/forums/t705692-class-member-static-array-of-pointers-null-initialization.html)

Ajay 0x007 11-18-2009 04:56 AM

class member "static array of pointers" NULL initialization
 
Hi

Does C++ standard says thay all elements of static array inside a
class are by defaults initialized by NULL??
i.e.

class myClass1{
public:
int i;
};
class myClass2 {
public:
static myClass1 arr[200];
};

myClass1 * myClass2::arr[200]; // will all 200 pointer elements be
always initialized to NULL or If not how it can be done ???

int main(void){
myClass2 obj;

}

Rolf Magnus 11-18-2009 09:01 AM

Re: class member "static array of pointers" NULL initialization
 
Ajay 0x007 wrote:

> Hi
>
> Does C++ standard says thay all elements of static array inside a
> class are by defaults initialized by NULL??


It says that objects with static storage are default initialized if you
don't explicitly initialize them. For non-POD types, this means zero
initialization. For non-PODs, the default constructor is used.

> i.e.
>
> class myClass1{
> public:
> int i;
> };
> class myClass2 {
> public:
> static myClass1 arr[200];
> };
>
> myClass1 * myClass2::arr[200]; // will all 200 pointer elements be
> always initialized to NULL


Yes.


Ajay 0x007 11-18-2009 09:14 AM

Re: class member "static array of pointers" NULL initialization
 
On Nov 18, 2:01*pm, Rolf Magnus <ramag...@t-online.de> wrote:
> Ajay 0x007 wrote:
> > Hi

>
> > Does C++ standard says thay *all elements of static array inside a
> > class are by defaults initialized by NULL??

>
> It says that objects with static storage are default initialized if you
> don't explicitly initialize them. For non-POD types, this means zero
> initialization. For non-PODs, the default constructor is used.
>
> > i.e.

>
> > class myClass1{
> > public:
> > int i;
> > };
> > class myClass2 {
> > public:
> > static myClass1 arr[200];
> > };

>
> > myClass1 * myClass2::arr[200]; // will all 200 pointer elements be
> > always initialized to NULL

>
> Yes.


Strangely with different "standard gcc versions" I am getting
different behaviours. Sometimes it is getting initialized to NULL and
sometimes its not.
I tried browsing C++ standard document but could not find any such
reference.
I request you to please confirm once that is this confirmed by C++
standard that all elemens of static array of pointer type (to user
defined data type) are automatically initialized to NULL (not 0)

Thanks in advace!

Andrey Tarasevich 11-18-2009 05:49 PM

Re: class member "static array of pointers" NULL initialization
 
Ajay 0x007 wrote:
> I request you to please confirm once that is this confirmed by C++
> standard that all elemens of static array of pointer type (to user
> defined data type) are automatically initialized to NULL (not 0)


All non-local objects with static storage duration in C++ program are
zero-initialized at the program startup. This initialization is carried
out before any other initialization begins. That's what 3.6.2/1 says.

It doesn't matter whether this is a static member of a class, a global
variable or anything else, as long as it has static storage duration.

Zero-initialization is a relatively high-level concept in C++. To
zero-initialize a pointer means to initialize it with the appropriate
null-pointer value (not just set it to all-zero bit pattern). This
applies to objects of all other types as well. (Meanwhile your remark
about "NULL (not 0)" is really meaningless, because in C++ 'NULL' is
equivalent to integral constant expression with zero value)

--
Best regards,
Andrey Tarasevich


All times are GMT. The time now is 10:39 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.