Connecting Tech Pros Worldwide Help | Site Map

c++: dynamic array

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 19th, 2005, 07:44 PM
A
Guest
 
Posts: n/a
Default c++: dynamic array

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, 07:44 PM
Mark Kerns
Guest
 
Posts: n/a
Default Re: 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, 07:44 PM
Andrew Koenig
Guest
 
Posts: n/a
Default Re: 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.


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.