c++: dynamic array

A

A

Hi,

consider the following code:

int i = new int[10];
myArray* a = new myArray[10];

question: sometimes you need to initialise all elements in an array to 0 (eg
using a for loop) before you can start adding items to the array, and other
times you just declare it and use it straight up. My question is, when do
you know when to do it?

regards
A
 
M

Mark Kerns

consider the following code:
int i = new int[10];
myArray* a = new myArray[10];

question: sometimes you need to initialise all elements in an array to 0 (eg
using a for loop) before you can start adding items to the array, and other
times you just declare it and use it straight up. My question is, when do
you know when to do it?

Well you should understand that for a (local) array of fundamental types
(AKA POD=Plain Old Data types such int, long, etc.), each element will
normally contain garbage (an uninitialized value) and you shouldn't carry
out any processing based on these elements until you later initialize them.
You can safely do this at your own convenience, looping through all of them
and initializing each, or you can initialize items 1, 3 and 8 only if you
really want. It makes no difference so long as you don't use a given element
until it has been initialized. The timing is up to you and your app's
requirements (initializing them all at once is common however). Your "i"
array is such an example since an "int" qualifies as a POD type. Each "int"
will initially contain garbage noting that this applies to local arrays only
however (as stated). For global arrays (those defined outside a function
essentially), each element *will* be initialized to zero for you (since this
applies to global variables in general, not just arrays). Note that for an
array of non-POD types however (your own classes basically), the default
constructor for each element is called when the array is created (unlike the
POD case). So each element will already be initialized based on the default
constructor for that class. Note that it's still up to you to initialize any
POD members of your class however (in the constructor) and you normally
should. Otherwise they'll also contain garbage unlike the non-POD members
whose default constructors will be called. The bottom line is that an
array's elements need not be initialized until you actually need them.
 
A

Andrew Koenig

consider the following code:
int i = new int[10];
myArray* a = new myArray[10];
question: sometimes you need to initialise all elements in an array to 0 (eg
using a for loop) before you can start adding items to the array, and other
times you just declare it and use it straight up. My question is, when do
you know when to do it?

You're writing the program, so you should know what you intend to do :)

More seriously, why are you using "new" at all? If you did it this way:

std::vector<int> i;

then the vector would start out with no elements at all, so you wouldn't
have to worry about how to initialize them. Then, when you wanted to add
items to the vector, you would write something like this:

i.push_back(n);

and the vector would have one element more than it did before.
 

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

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top