class member "static array of pointers" NULL initialization

Discussion in 'C++' started by Ajay 0x007, Nov 18, 2009.

  1. Ajay 0x007

    Ajay 0x007 Guest

    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;

    }
    Ajay 0x007, Nov 18, 2009
    #1
    1. Advertising

  2. Ajay 0x007

    Rolf Magnus Guest

    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.
    Rolf Magnus, Nov 18, 2009
    #2
    1. Advertising

  3. Ajay 0x007

    Ajay 0x007 Guest

    On Nov 18, 2:01 pm, Rolf Magnus <> 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!
    Ajay 0x007, Nov 18, 2009
    #3
  4. 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
    Andrey Tarasevich, Nov 18, 2009
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,742
    Thomas Weidenfeller
    Oct 12, 2005
  2. Replies:
    5
    Views:
    26,610
    Mike Schilling
    Mar 29, 2006
  3. Replies:
    3
    Views:
    846
    Peter_Julian
    Oct 10, 2005
  4. Angus
    Replies:
    1
    Views:
    2,628
  5. aaragon
    Replies:
    2
    Views:
    615
    James Kanze
    Nov 2, 2008
Loading...

Share This Page