By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,921 Members | 1,479 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,921 IT Pros & Developers. It's quick & easy.

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

P: n/a
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[i].first = 0.0;
foo[i].second = 0.0;
foo[i].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

Oct 24 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
* 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[i].first = 0.0;
foo[i].second = 0.0;
foo[i].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?
Oct 24 '06 #2

P: n/a
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[i].first = 0.0;
foo[i].second = 0.0;
foo[i].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?
Oct 24 '06 #3

P: n/a
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
Oct 24 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.