Connecting Tech Pros Worldwide Help | Site Map

c++: dynamic array

  #1  
Old July 19th, 2005, 08:44 PM
A
Guest
 
Posts: n/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



  #2  
Old July 19th, 2005, 08:44 PM
Mark Kerns
Guest
 
Posts: n/a

re: c++: dynamic array


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

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.


  #3  
Old July 19th, 2005, 08:44 PM
Andrew Koenig
Guest
 
Posts: n/a

re: c++: dynamic array


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

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.


Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
C99 dynamic array kwikius answers 13 February 18th, 2008 01:35 AM
How can you quickly find the number of elements stored in a a) staticarray b) dynamic array ? C C++ C++ answers 11 January 17th, 2008 09:35 AM
dynamic array of pointers vs dynamic array of objects lemonade answers 1 November 16th, 2005 10:49 PM
Can a static array contain a dynamic array of pointers? Peter B. Steiger answers 8 November 14th, 2005 06:15 AM
dynamic array or STL vector Vasileios Zografos answers 6 July 22nd, 2005 04:47 AM