Initializing an array of a structure to default value, does compiler do a loop to initialize?

Discussion in 'C++' started by vduber6er, Oct 24, 2006.

  1. vduber6er

    vduber6er Guest

    Lets say I have this structure:

    typedef struct numbers {
    double first = 0.0;
    double second = 0.0;
    double third = 0.0;
    } MYVALUES;

    and I initialize an array of MYVALUES like the following

    MYVALUES * foo;
    foo = new MYVALUES [3];

    I would expect the following initial values from foo:

    foo[0].first == 0.0
    foo[0].second == 0.0
    foo[0].third == 0.0
    foo[1].first == 0.0
    foo[1].second == 0.0
    foo[1].third == 0.0
    foo[2].first == 0.0
    foo[2].second == 0.0
    foo[2].third == 0.0

    My question is, how does the compiler initialize these values? Does it
    run a loop to initialize foo kinda like this?

    for (i=0; i<3; i++){
    foo.first = 0.0;
    foo.second = 0.0;
    foo.third = 0.0;
    }

    I can't have it run a loop because my array can potentially be very
    large and i don't want it to take up processing time.

    Thanks

    Eric
     
    vduber6er, Oct 24, 2006
    #1
    1. Advertising

  2. Re: Initializing an array of a structure to default value, does compilerdo a loop to initialize?

    * vduber6er:
    > Lets say I have this structure:
    >
    > typedef struct numbers {
    > double first = 0.0;
    > double second = 0.0;
    > double third = 0.0;
    > } MYVALUES;


    Don't use all uppercase except for macro names, where you should.

    Don't use typedef, it's a C'ism.

    Don't initialize in member declarations -- it shouldn't even compile
    (have you even /tried/ to compile the code?).

    In C++ write just

    struct numbers
    {
    double first, second, third;
    numbers(): first(0), second(0), third(0) {}
    };

    You can even omit the explicit 0's, since that's the default.



    > and I initialize an array of MYVALUES like the following
    >
    > MYVALUES * foo;
    > foo = new MYVALUES [3];
    >
    > I would expect the following initial values from foo:
    >
    > foo[0].first == 0.0
    > foo[0].second == 0.0
    > foo[0].third == 0.0
    > foo[1].first == 0.0
    > foo[1].second == 0.0
    > foo[1].third == 0.0
    > foo[2].first == 0.0
    > foo[2].second == 0.0
    > foo[2].third == 0.0


    With numbers declared as shown above that is indeed the result, because
    that's what's specified in the numbers default constructor.


    > My question is, how does the compiler initialize these values? Does it
    > run a loop to initialize foo kinda like this?
    >
    > for (i=0; i<3; i++){
    > foo.first = 0.0;
    > foo.second = 0.0;
    > foo.third = 0.0;
    > }


    Yes.


    > I can't have it run a loop because my array can potentially be very
    > large and i don't want it to take up processing time.


    No matter how it's done there will be a loop: there's no magic.

    --
    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?
     
    Alf P. Steinbach, Oct 24, 2006
    #2
    1. Advertising

  3. vduber6er

    vduber6er Guest

    Thanks Alf. Yeah I realized my structure was flawed after I wrote it
    down and was actually going to edit my post, but you beat me to it.
    Thanks for the answer. I guess there is no way to avoid a loop.

    Eric

    Alf P. Steinbach wrote:
    > * vduber6er:
    > > Lets say I have this structure:
    > >
    > > typedef struct numbers {
    > > double first = 0.0;
    > > double second = 0.0;
    > > double third = 0.0;
    > > } MYVALUES;

    >
    > Don't use all uppercase except for macro names, where you should.
    >
    > Don't use typedef, it's a C'ism.
    >
    > Don't initialize in member declarations -- it shouldn't even compile
    > (have you even /tried/ to compile the code?).
    >
    > In C++ write just
    >
    > struct numbers
    > {
    > double first, second, third;
    > numbers(): first(0), second(0), third(0) {}
    > };
    >
    > You can even omit the explicit 0's, since that's the default.
    >
    >
    >
    > > and I initialize an array of MYVALUES like the following
    > >
    > > MYVALUES * foo;
    > > foo = new MYVALUES [3];
    > >
    > > I would expect the following initial values from foo:
    > >
    > > foo[0].first == 0.0
    > > foo[0].second == 0.0
    > > foo[0].third == 0.0
    > > foo[1].first == 0.0
    > > foo[1].second == 0.0
    > > foo[1].third == 0.0
    > > foo[2].first == 0.0
    > > foo[2].second == 0.0
    > > foo[2].third == 0.0

    >
    > With numbers declared as shown above that is indeed the result, because
    > that's what's specified in the numbers default constructor.
    >
    >
    > > My question is, how does the compiler initialize these values? Does it
    > > run a loop to initialize foo kinda like this?
    > >
    > > for (i=0; i<3; i++){
    > > foo.first = 0.0;
    > > foo.second = 0.0;
    > > foo.third = 0.0;
    > > }

    >
    > Yes.
    >
    >
    > > I can't have it run a loop because my array can potentially be very
    > > large and i don't want it to take up processing time.

    >
    > No matter how it's done there will be a loop: there's no magic.
    >
    > --
    > 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?
     
    vduber6er, Oct 24, 2006
    #3
  4. vduber6er posted:

    > typedef struct numbers {
    > double first = 0.0;
    > double second = 0.0;
    > double third = 0.0;
    > } MYVALUES;
    >
    > and I initialize an array of MYVALUES like the following
    >
    > MYVALUES * foo;
    > foo = new MYVALUES [3];
    >
    > I would expect the following initial values from foo:
    >
    > foo[0].first == 0.0
    > foo[0].second == 0.0
    > foo[0].third == 0.0
    > foo[1].first == 0.0
    > foo[1].second == 0.0
    > foo[1].third == 0.0
    > foo[2].first == 0.0
    > foo[2].second == 0.0
    > foo[2].third == 0.0



    You can go with Alf's solution, but I prefer:

    struct Whatever {
    double first,second,third;
    };

    Whatever *const p = new Whatever[3]();

    The parentheses behind the final semi-colon indicate that the array is to
    be default-initialised.

    --

    Frederick Gotham
     
    Frederick Gotham, Oct 24, 2006
    #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. Replies:
    11
    Views:
    1,118
  2. Chandrashekar Tippur

    Initializing array of structure!!

    Chandrashekar Tippur, Nov 19, 2004, in forum: C Programming
    Replies:
    7
    Views:
    1,907
    Jack Klein
    Nov 20, 2004
  3. Replies:
    1
    Views:
    366
    Victor Bazarov
    Nov 6, 2007
  4. RB
    Replies:
    4
    Views:
    1,512
  5. Isaac Won
    Replies:
    9
    Views:
    390
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page