Wiki

Clone wiki

sig / GsArray

GsArray

One of the most used classes in SIG is GsArray, which is an important class designed to efficiently manipulate memory.

The class works similarly to std::vector; however, it has a main difference: it manipulates the internal data of the array as non-typed memory blocks. The elements of the array are therefore not treated as objects and the constructors, destructors, copy operators, etc. of the elements in the array are never called when the array is manipulated.

Since GsArray relies on low-level C functions for memory allocation, re-allocation, and deletion, GsArray outperforms std::vector in several operations. For example, see the 'array' test provided in SIG's test application 'gstests'. Here are some numbers that were obtained:

GsArray time: 0.350413 secs
std::vector : 0.941436 secs

The user needs however to pay attention to only use it with primitive types, or for pointers to objects with the use of GsArrayPt. See gs_array.h for details.

It is also important to observe that GsArray automatically doubles its internal memory when it needs more space during operations. So references to objects in the array will only be guaranteed to be valid while no new elements are added to the array.

For example, a command sequence like:

#!c++
int& e=array[0];
array.push()=number; // a push() may trigger memory reallocation to increase the array capacity
e=new_value;         // the previous reference e may now point to invalid memory!

should never be written because when 'number' is pushed to the array memory re-allocation may happen, in which case reference e to element index 0 will become invalid.

Updated