Auto-initialisation of arrays

G

Guest

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

Victor Bazarov

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
 
J

John Carson

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).
 
B

Bernd Strieder

Hello,

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
 
P

Peter Julian

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 << 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
*/
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top