Skip to content

CArrayObj

CArrayObj

CArrayObj class is a class of dynamic array of pointers to instances of CObject and its derived classes.

Description

Class CArrayObj provides the ability to work with a dynamic array of pointers to instances of CObject and its derived classes. This allows working both with multidimensional dynamic arrays of primitive data types and with data structures that have more complex organization of data.

The class allows adding/inserting/deleting array elements, performing an array sorting, and searching in a sorted array. In addition, methods of working with files have been implemented.

There are certain subtleties of the class CArrayObj.

Declaration

class CArrayObj : public CArray

Title

#include <Arrays\ArrayObj.mqh>

Inheritance hierarchy

CObject

CArray

CArrayObj

Direct descendants

: CIndicators, CSeries

Class Methods by Groups

Attributes
FreeModeGets the flag of memory management
FreeModeSets the flag of memory management
Memory control
ReserveAllocates memory to increase the size of the array
ResizeSets a new (smaller) size of the array
ShutdownClears the array with full deallocation of its memory (but not its elements).
Creating a new element
virtual CreateElementCreates a new array element in the specified position
Add methods
AddAdds an element to the end of the array
AddArrayAdds an element to the end of the array
InsertInserts an element to the specified position in the array
InsertArrayInserts to an array elements from another array from the specified position
AssignArrayCopies the elements of one array to another
Update methods
UpdateChanges the element at the specified position array
ShiftMoves an item from a given position in the array to the specified offset
Delete methods
DetachGets the element from the specified position and removes it from the array
DeleteRemoves the element from the specified array position
DeleteRangeDeletes a group of elements from the specified array position
ClearRemoves all elements of the array without the release of the array memory
Access methods
AtGets the element from the specified array position
Compare methods
CompareArrayCompares the array with another one
Sorted array operations
InsertSortInserts an element in a sorted array
SearchSearches for an element equal to the sample in the sorted array
SearchGreatSearches for an element with a value exceeding the value of the sample in the sorted array
SearchLessSearches for an element with a value less than the value of the sample in the sorted array
SearchGreatOrEqualSearches for an element with a value greater than or equal to the value of the sample in the sorted array
SearchLessOrEqualSearches for an element with a value less than or equal to the value of the sample in the sorted array
SearchFirstSearches for the first element equal to the sample in the sorted array
SearchLastSearches for the last element equal to the sample in the sorted array
Input/output
SaveSaves data array in the file
LoadLoads data array from the file
TypeGets the type identifier of the array

Methods inherited from class CArray

: Step, Step, Total, Available, Max, IsSorted, SortMode, Clear, Sort

Arrays of the CObject class have practical application (including all classes of the Standard Library).

For example, consider the options for two-dimensional array:

#include <Arrays\ArrayDouble.mqh>
#include <Arrays\ArrayObj.mqh>
//---
void OnStart()
  {
   int i,j;
   int first_size=10;
   int second_size=100;
//--- create array
   CArrayObj    *array=new CArrayObj;
   CArrayDouble *sub_array;
//---
   if(array==NULL)
     {
      printf("Object create error");
      return;
     }
//--- create subarrays
   for(i=0;i<first_size;i++)
     {
      sub_array=new CArrayDouble;
      if(sub_array==NULL)
        {
         delete array;
         printf("Object create error");
         return;
        }
      //--- fill array
      for(j=0;j<second_size;j++)
        {
         sub_array.Add(i*j);
        }
      array.Add(sub_array);
     }
//--- create array OK
   for(i=0;i<first_size;i++)
     {
      sub_array=array.At(i);
      for(j=0;j<second_size;j++)
        {
         double element=sub_array.At(j);
         //--- use array element
        }
     }
   delete array;
  }

Subtleties

The class has a mechanism to control dynamic memory, so be careful when working with elements of the array.

Mechanism of memory management can be switched on/off using the method FreeMode (bool). By default, the mechanism is enabled.

Accordingly, there are two options for dealing with the CArrayObj class:

  1. Mechanism of memory management is enabled. (default)

In this case, CArrayObj takes responsibility for releasing the memory used for the elements after their removal from the array. A custom program should not release the array elements.

Example:

int i;
//--- create an array
   CArrayObj *array=new CArrayObj;
//--- fill array elements
   for(i=0;i<10;i++) array.Add(new CObject);
//--- do something
   for(i=0;i<array.Total();i++)
     {
      CObject *object=array.At(i);
      //--- actions performed with the element
      . . .
     }
//--- remove the array with the elements
   delete array;
  1. Mechanism of memory management is disabled.

In this case, CArrayObj is not responsible for deallocating of the elements’ memory after their removal from the array. Besides, the user program must deallocate the array elements.

Example:

int i;
//--- create an array
   CArrayObj *array=new CArrayObj;
//--- disable the mechanism of memory management
   array.FreeMode(false);
//--- fill array with elements
   for(i=0;i<10;i++) array.Add(new CObject);
//--- do something
   for(i=0;i<array.Total();i++)
     {
      CObject *object=array.At(i);
      //--- actions performed with the element
      . . .
     }
//--- remove array elements
   while(array.Total()) delete array.Detach();
//--- remove empty array
   delete array;
Last updated on