[Insight-users] itk::Array

Luis Ibanez luis.ibanez@kitware.com
Wed, 29 Jan 2003 10:55:47 -0500


Hi Kumar,

I agree with you in that it could be convenient
to initialize arrays to a specific value.
This is particularly relevant for float/double
types.

However, in dynamic arrays, this can take some
computing time if the array is large. This time
will be wasted if the next operation you do is
to initialize the array by some other method.
Only the user of the Array can anticipate this.

The simple solution is to explicitly initialize
using the method Fill(). That seems to be a
reasonable solution that satisfy all the needs.

This is a common practice following the C-language
principle of "you only pay for what you use".
If you look at STL for example, containers are not
initialized either (other by the actions of the
default constructor of the elements).

Take also into account that itk::Array<> is a
templated class. So the array element can be
*anything*. The elements could be 4D meshes for
example. It is less clear in this case what
the correct initialization should be.



     Luis


-----------------------------------------
kumar wrote:
> Hi Luis
> Thanks for the comments,
> 
> I am forced to use itk::Array due to its advantages of dynamic construction
> The memory allocation part is fine and I follow the same
> 
> But what is puzzling me is when the memory allocation is done
> dont  the array values get initialized to some value, say zero
> 
> Or is it that one  has to do  initialize the array values for each array 
> one creates
> 
> Thanks and Regards
> Kumar
> 
> 
> Luis Ibanez wrote:
> 
>>
>>
>> Hi Kumar,
>>
>>
>> itk::Array is a dynamic array.
>> It derives from the vnl_vector<>.
>>
>> This class requires initialization for
>> its size and memory allocation.
>>
>> If you have a constant number of elements
>> you may want to consider using itk::FixedArray
>> instead.
>>
>> itk::Array can be initialized by giving a size
>> argument to the constructor, like
>>
>>        itk::Array<double>   A( 100 );
>>
>> or by assignment from another array, like
>>
>>        itk::Array<double>   B;
>>
>>        B = A;
>>
>> This is as far as memory allocation goes.
>> Now, for initializing the array values, you
>> may use the Fill() method.  Like
>>
>>
>>       B.Fill(  0.0  );
>>
>> or more generic-programming-like:
>>
>>
>>   B.Fill( itk::NumericTraits<double>::Zero );
>>
>>
>>
>> Please let us know if you have further questions,
>>
>>
>> Thanks
>>
>>
>> Luis
>>
>>
>>
>>
> 
> 
>