class member "static array of pointers" NULL initialization

A

Ajay 0x007

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;

}
 
R

Rolf Magnus

Ajay said:
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.
 
A

Ajay 0x007

Ajay said:
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.
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!
 
A

Andrey Tarasevich

Ajay said:
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)
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top