(!) Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.

Arrays

Description: Introduction to c++ style arrays, both fixed and dynamic.

Keywords: ecl containers arrays

Tutorial Level: INTERMEDIATE

Next Tutorial: Stencils Buffer Overflow Detection

Overview

Ecl arrays provide a typical c++ style stl interface. There are, however a couple of features of arrays that stand out from stl containers.

  • Fixed arrays store data on the stack -> faster for small arrays.

  • _Comma Initialisation_ : eigen style initialisation.
  • _Blueprint Instantiation_ : efficient construction without costly copies.

Fixed Arrays

Fixed arrays provide a fixed size container (enabled via a template parameter) that can be saved as contiguous memory on the stack. This is in contrast to typical stl containers that are dynamic and thus use the heap for storage, which is typically much slower. This is especially true for small arrays, though other considerations become more important as the array size increases.

Comma Initialisation

This concept is brought in (with slightly modified form) from Blitz++ and Eigen.

   1 ecl::Array<int,5> array; // be careful, at this point, the contents are uninitialised.
   2 array << 1,2,3,4,5;      // comma initialisation
   3 

Comman initialisers will throw an exception if the range is exceeded in debug mode (beware release mode!).

BluePrint Instantiations

These provide an efficient means of instantiating array classes without a costly copy-on-the-fly slowdown. It uses a blueprint coupled with what is referred to as a blueprint factory to do this (refer to ecl_concepts for more details.). However, the construction makes the usage of this transparent.

   1 // construction.
   2 Array<int,5> array = Array<int,5>::Constant(3); 
   3 // assignment, no costly copy operation used.
   4 array = Array<int,5>::Constant(3);              

Dynamic Arrays

These arrays are almost exactly the same as the former fixed arrays, however they are dynamically sizeable (though not as automatically nor flexibly as vectors) with their storage on the heap, not the stack. The lack of flexibility isn't a serious disadvantage as a control program should generally manage vector memory careful (read, manually!) anyway. Their other feature is again, like the fixed arrays, the _comma initialiser_.

   1 // Construct without storage allocated.
   2 Array<int> array1;    
   3 // Initialise a vector with storage space for four integers.
   4 Array<int> array2(4); 
   5 // Initialise space for four integers filled with a value of 3.
   6 Array<int> array3 = Array<int>::Constant(4,3); 
   7 
   8 array1.clear();       // Clear - delete and point buffer to NULL.
   9 array1.resize(10);    // Resize - delete and recreate uninitialised buffer.
  10 

Wiki: ecl_containers/Tutorials/Arrays (last edited 2012-01-18 14:31:13 by DanielStonier)