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

V

vduber6er

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
 
A

Alf P. Steinbach

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

vduber6er

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
* 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?
 
F

Frederick Gotham

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.
 

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,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top