When LocalVar_TestScript_2.mq5 is run for the first time, it gives only
PO 0 LocalVar_TestScript_2 (EURGBP,M1) 12:08:19 CItem::CItem Constructor LD 0 LocalVar_TestScript_2 (EURGBP,M1) 12:08:19 CItem::CItem Constructor HJ 0 LocalVar_TestScript_2 (EURGBP,M1) 12:08:19 CItem::CItem Constructor DS 0 LocalVar_TestScript_2 (EURGBP,M1) 12:08:19 CItem::CItem Constructor PH 0 LocalVar_TestScript_2 (EURGBP,M1) 12:08:19 CItem::CItem Constructor GP 0 LocalVar_TestScript_2 (EURGBP,M1) 12:08:19 CItem::~CItem Destructor KF 0 LocalVar_TestScript_2 (EURGBP,M1) 12:08:19 CItem::~CItem Destructor OM 0 LocalVar_TestScript_2 (EURGBP,M1) 12:08:19 CItem::~CItem Destructor CD 0 LocalVar_TestScript_2 (EURGBP,M1) 12:08:19 CItem::~CItem Destructor GK 0 LocalVar_TestScript_2 (EURGBP,M1) 12:08:19 CItem::~CItem Destructor
i.e. without the message"delete invalid pointer". After recompiling the same file (i.e. after the second compilation), it shows this:
PQ 0 LocalVar_TestScript_2 (EURUSD,H1) 12:09:12 CItem::CItem Constructor LH 0 LocalVar_TestScript_2 (EURUSD,H1) 12:09:12 CItem::CItem Constructor HO 0 LocalVar_TestScript_2 (EURUSD,H1) 12:09:12 CItem::CItem Constructor DF 0 LocalVar_TestScript_2 (EURUSD,H1) 12:09:12 CItem::CItem Constructor PL 0 LocalVar_TestScript_2 (EURUSD,H1) 12:09:12 CItem::CItem Constructor GR 0 LocalVar_TestScript_2 (EURUSD,H1) 12:09:12 CItem::~CItem Destructor KK 0 LocalVar_TestScript_2 (EURUSD,H1) 12:09:12 CItem::~CItem Destructor OP 0 LocalVar_TestScript_2 (EURUSD,H1) 12:09:12 CItem::~CItem Destructor CF 0 LocalVar_TestScript_2 (EURUSD,H1) 12:09:12 CItem::~CItem Destructor GO 0 LocalVar_TestScript_2 (EURUSD,H1) 12:09:12 CItem::~CItem Destructor PH 1 LocalVar_TestScript_2 (EURUSD,H1) 12:09:12 敤敬整渠湯搠湹浡捩漠橢捥t RP 1 LocalVar_TestScript_2 (EURUSD,H1) 12:09:12 敤敬整渠湯搠湹浡捩漠橢捥t DH 1 LocalVar_TestScript_2 (EURUSD,H1) 12:09:12 敤敬整渠湯搠湹浡捩漠橢捥t FP 1 LocalVar_TestScript_2 (EURUSD,H1) 12:09:12 敤敬整渠湯搠湹浡捩漠橢捥t HH 1 LocalVar_TestScript_2 (EURUSD,H1) 12:09:12 敤敬整渠湯搠湹浡捩漠橢捥tHas the terminal's reaction to deleting invalid pointers been changed since the article was published? XP, 32
When LocalVar_TestScript_2.mq5 is run for the first time, it gives only
i.e. without the message"delete invalid pointer". After recompiling the same file (i.e. after the second compilation), it shows this:
Has the terminal's reaction to deleting invalid pointers been changed since the article was published? XP, 32mql5:
Спасибо за сообщение. Исправлено. Поведение изменилось, теперь сообщение "delete invalid pointer" выдаётся только при компиляции под отладку.
Why does this line cause the constructor to run?
CItem* array1[5];
and this one doesn't:
CObjectC *pObjectC;?

- www.mql5.com
Why this line causes the constructor to be called:
And why this line causes the constructor to run: CItem* array1[5];,
but this one does not: CObjectC *pObjectC; ?
I understood that the line
CItem* array1[5];
by itself does not cause the constructor to be launched. Here is more complete code from the article:
void OnStart() { //--- declare the first array of pointers to the object CItem* array1[5]; //--- declare the second array of pointers to the object CItem* array2[5]; //--- now let's fill the arrays in the loop for(int i=0;i<5;i++) { //--- pointer for the first array will be created by the operator new array1[i]=new CItem; //--- the pointer for the second array will be copied from the first array array2[i]=array1[i]; }The constructor is called not when declaring the first pointer array, but when filling this array in a loop using the new operator.
The new operator is needed only to make the class dynamic? But if a dynamic array is "an array with an unspecified value in the first pair of square brackets", what is a dynamic class?
But still there are questions:
What is the point of creating a dynamic object via the new operator?
When creating an object automatically, the class object is created in the stack, it is faster than dynamic object by execution time.
When creating an object dynamically, the class object is created in memory (in the heap) while using the OS memory manager, the process is slower.
Here's a question: if automatic creation is faster, why are dynamic objects created? To prevent the stack from overflowing?

- 2010.02.25
- MetaQuotes Software Corp.
- www.mql5.com

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
New article The Order of Object Creation and Destruction in MQL5 is published:
Every object, whether it is a custom object, a dynamic array or an array of objects, is created and deleted in MQL5-program in its particular way. Often, some objects are part of other objects, and the order of object deleting at deinitialization becomes especially important. This article provides some examples that cover the mechanisms of working with objects.
MQL5-programs are written in concepts of Object Oriented Programming (OOP), and that not only opens up new possibilities for creating custom libraries, but also allows you to use complete and tested classes of other developers. In the Standard Library, that is included into the MetaTrader 5 Client Terminal, there are hundreds of classes that contain thousands of methods.
To take full advantages of OOP we must clarify some details about creating and deleting objects in MQL5 programs. Creating and Deleting Objects is briefly described in Documentation, and this article will illustrate this topic in examples.Author: MetaQuotes