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

Auto-initialisation of arrays

P: n/a
This question has just occurred to me - but if I declare an array of
numeric variables(eg int goats[47], double *wasps=new double[4]), are
the values of it's elements automatically set to zero? If so, is there
any way to save program time by stopping this happening? (and if yes,
I can remove a good few unnecessary loops)

Thanks,

James M.
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
ze*****************@yahoo.com wrote:
This question has just occurred to me - but if I declare an array of
numeric variables(eg int goats[47], double *wasps=new double[4]), are
the values of it's elements automatically set to zero? If so, is there
any way to save program time by stopping this happening? (and if yes,
I can remove a good few unnecessary loops)


It depends on whether you give it an initialiser or not. Try to see
the difference in

int goats[47] = {};

and

int goats[47];

and also between

double *wasps = new double[4]();

and

double *wasps = new double[4];

(Oh, it's worth mentioning that not all compilers can handle those)

V
Jul 23 '05 #2

P: n/a
<ze*****************@yahoo.com> wrote in message
news:qb********************************@4ax.com
This question has just occurred to me - but if I declare an array of
numeric variables(eg int goats[47], double *wasps=new double[4]), are
the values of it's elements automatically set to zero?
No.
If so, is there
any way to save program time by stopping this happening? (and if yes,
I can remove a good few unnecessary loops)


You can ensure zero initialisation in the first case with:

int goats[47] = {0};

In the second case

double *wasps=new double[4]();

(note the final parentheses) has the desired effect with VC++, but I don't
know if it is standard (it is standard if it is just new double(); but I am
not sure if it is meant to work with arrays).
--
John Carson

Jul 23 '05 #3

P: n/a
Hello,

ze*****************@yahoo.com wrote:
This question has just occurred to me - but if I declare an array of
numeric variables(eg int goats[47], double *wasps=new double[4]), are
the values of it's elements automatically set to zero? If so, is there
any way to save program time by stopping this happening? (and if yes,
I can remove a good few unnecessary loops)


How on earth would memory be zero-initialized, if not with some kind of
a loop. Although there might be faster ways to zero-initialize an array
than a programmed loop in C++, this should not matter, or you have more
serious problems.

Basically, you ask for a convenient way to handle arrays of variable
size. Instead of zero-initializing a sufficient amount of elements, you
should add elements as soon as they occur. This saves you overwriting
the same element twice, once the zero, and next time the intended
value. Therefore, use std::vector with its automatic resizing via
push_back instead of arrays, then you need not think about
zero-initializing before the first actual use. The interface of
std::vector is a lot friendlier than that of arrays.

Bernd Strieder

Jul 23 '05 #4

P: n/a

<ze*****************@yahoo.com> wrote in message
news:qb********************************@4ax.com...
This question has just occurred to me - but if I declare an array of
numeric variables(eg int goats[47], double *wasps=new double[4]), are
the values of it's elements automatically set to zero? If so, is there
any way to save program time by stopping this happening? (and if yes,
I can remove a good few unnecessary loops)


Arrays are generated at compile time with a fixed size.

In the case you're interested in having run-time control over the container,
try a std::vector or std::deque. You'll think twice about using an array
again.

#include <iostream>
#include <vector>

int main()
{
std::vector<double> vd(6); // a vector of doubles with 6 elements

vd.push_back(0.0); // push_back four more elements
vd.push_back(0.0);
vd.push_back(0.0);
vd.push_back(0.0);

for (size_t i = 0; i < vd.size(); ++i)
{
std::cout << "vd[" << i << "] = " << vd[i] << std::endl;
}

std::cout << "vector size = " << vd.size() << std::endl;

return 0;
}

/*
vd[0] = 0
vd[1] = 0
vd[2] = 0
vd[3] = 0
vd[4] = 0
vd[5] = 0
vd[6] = 0
vd[7] = 0
vd[8] = 0
vd[9] = 0
vector size = 10
*/

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.