Re: dynamic array

From: Mark Kerns (_nospam_at__nospam.com)
Date: 10/28/03


Date: Tue, 28 Oct 2003 14:11:38 GMT


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



Relevant Pages

  • Re: Arrays!
    ... > innitialize a two dimensional array in my constructor of my class. ... you declare and initialize a local variable named ... There is no syntax in C++ to initialize an array that is a member ...
    (microsoft.public.vc.language)
  • Re: cannot have instance field initializers in structs
    ... implement my own default constructor or to manually initialize field ... it would take to allocate my struct. ... if it takes an hour to initialize this ... > array of references and therefore the allocation is always a known thing. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Arrays!
    ... > trying to in the constructor. ... and just have the declaration of the array done ... >> from the array within any accessor function. ... > If you make the array static, initialize it like I did in T3. ...
    (microsoft.public.vc.language)
  • Re: compiler bugs
    ... In a language like C, the behavior of programs that reference ... Not if the unoptimizing compiler initializes all variables to some ... portion of the array in your algorithhm. ... that one actually does not need to initialize all variables (unlike ...
    (comp.compilers)
  • Re: Error 216 (related to SetLength)
    ... It might turn out that you're writing to a part of an array that doesn't exist. ... suggests you're talking about an access violation (in which case I'd like to know what addresses the exception reports), but your previous message only quoted the text from a different kind of exception, about changing visibility in OnShow or OnHide. ... I'd even go so far as to argue that OnCreate isn't the right place, ... Since your variables are global, they aren't related to any specific form, so a form isn't what should initialize them. ...
    (alt.comp.lang.borland-delphi)