Are arrays of mql5 structs stored reliable complete in one piece in memory?

 

Hi, I can't find any statement about this in the documentation.

The question:
Are static+dynamic arrays of structs like MqlTick and MqlRates in mql5 in memory always complete in one piece (like C++ std::vector)?
So that elements/arrays can be accessed e.g. from dll using a pointer?

Or is it possible that the arrays of MqlTick+MqlRates are NOT stored contiguously in RAM (like C++ std::deque) and thus the access by pointer can be dangerous?

Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Price Data Structure
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Price Data Structure
  • www.mql5.com
Price Data Structure - Data Structures - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
4lpha:

Hi, I can't find any statement about this in the documentation.

The question:
Are static+dynamic arrays of structs like MqlTick and MqlRates in mql5 in memory always complete in one piece (like C++ std::vector)?
So that elements/arrays can be accessed e.g. from dll using a pointer?

Or is it possible that the arrays of MqlTick+MqlRates are NOT stored contiguously in RAM (like C++ std::deque) and thus the access by pointer can be dangerous?

Arrays are always contiguous, the only thing to be aware is when you resize them, the starting address might change.

Since MqlTick and MqlRates are considered simple structures their bytes can even be copied directly to another structure
and the compiler does that when you assign them.

Documentation on MQL5: Language Basics / Data Types / Structures, Classes and Interfaces
Documentation on MQL5: Language Basics / Data Types / Structures, Classes and Interfaces
  • www.mql5.com
Structures, Classes and Interfaces - Data Types - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Alexandre Borela #:

Arrays are always contiguous, the only thing to be aware is when you resize them, the starting address might change.

Since MqlTick and MqlRates are considered simple structures their bytes can even be copied directly to another structure
and the compiler does that when you assign them.


Ok thanks that sounds good but how do you know for sure that it's really always in the ram in one piece? have you tried it statistically repeated (100%) or is there a metaquotes statement?
Because the statement "Arrays are always contiguous" related to RAM is not true in every programming language (best example c++ std::deque).

 
I suppose you are referring to Windows OS.


They are in one piece.

Your example is not an array. Take a look at the definition of std::deque



 
Dominik Christian Egert #:
I suppose you are referring to Windows OS.


They are in one piece.

Your example is not an array. Take a look at the definition of std::deque



Yes in all C++ references this is all clearly described but not in MQL5 reference.
MQL5 is not C++, MQL5 is an own scripting language. How do you know it for sure that the sequential container in MQL5 like arrays of structs(MqlTick/MqlRates) is in one piece in the ram?
 

Forum on trading, automated trading systems and testing trading strategies

Are arrays of mql5 structs stored reliable complete in one piece in memory?

4lpha, 2022.01.06 16:42


Ok thanks that sounds good but how do you know for sure that it's really always in the ram in one piece? have you tried it statistically repeated (100%) or is there a metaquotes statement?
Because the statement "Arrays are always contiguous" related to RAM is not true in every programming language (best example c++ std::deque).

A deque is not an array. It would be pretty dumb for metaquotes to create MQL based on C++ but use deque or list as the underlying structure for the arrays,
I am not even considering the performance implications.

I also implemented C++'s containers/iterators in MQL for my libraries and arrays are working just like the C++ ones.

Forum on trading, automated trading systems and testing trading strategies

Are arrays of mql5 structs stored reliable complete in one piece in memory?

4lpha, 2022.01.06 17:32

Yes in all C++ references this is all clearly described but not in MQL5 reference.
MQL5 is not C++, MQL5 is an own scripting language. How do you know it for sure that the sequential container in MQL5 like arrays of structs(MqlTick/MqlRates) is in one piece in the ram?
Have you coded anything where it is not behaving like an C++ array?
 
Alexandre Borela #:
Have you coded anything where it is not behaving as an array?

Jes sure, the Array in MQL5 you can resize (https://www.mql5.com/de/docs/array/arrayresize). Resize is not possible with the real array in c++ (https://en.cppreference.com/w/cpp/container/array).
So the MQL5 array is not similar to the std::array but because the resize maybe to the std::vector or std::deque...
We do not know what exactly is behind the MQL5 array. Therefore the question.

I think you can't compare C++(low-level) and MQL5(interpreted high-level). There is only syntactic similarity nothing more.

Dokumentation zu MQL5: Operationen mit Arrays / ArrayResize
Dokumentation zu MQL5: Operationen mit Arrays / ArrayResize
  • www.mql5.com
ArrayResize - Operationen mit Arrays - Nachschlagewerk MQL5 - Nachschlagewerk über die Sprache des algothitmischen/automatischen Handels für MetaTrader 5
 
Alexandre Borela #:
I also implemented C++'s containers/iterators in MQL for my libraries and arrays are working just like the C++ ones.

This is a good indication that it is similar to std::vector (contiguously in RAM). But we don't have a real proof, do we?

 
4lpha #:

Jes sure, the Array in MQL5 you can resize (https://www.mql5.com/de/docs/array/arrayresize). Resize is not possible with the real array in c++ (https://en.cppreference.com/w/cpp/container/array).
So the MQL5 array is not similar to the std::array but because the resize maybe to the std::vector or std::deque...
We do not know what exactly is behind the MQL5 array. Therefore the question.

I think you can't compare C++(low-level) and MQL5(interpreted high-level). There is only syntactic similarity nothing more.

mql5 is not an interpreted language. It's compiled and managed language.

Of course mql arrays are contiguous in memory. It's implicit from the documentation and experience. If you want to prove it otherwise please do it and show us.

 
4lpha #:

This is a good indication that it is similar to std::vector (contiguously in RAM). But we don't have a real proof, do we?

No, again you are confusing containers with arrays. I said I implemented C++'s containers in MQL (one of them is the vector)
which uses arrays as its storage medium, and, the arrays are behaving as expected.

You still didn't answer my question, have you implemented something where the arrays are not behaving as expected?
 
4lpha #:

Jes sure, the Array in MQL5 you can resize (https://www.mql5.com/de/docs/array/arrayresize). Resize is not possible with the real array in c++ (https://en.cppreference.com/w/cpp/container/array).
So the MQL5 array is not similar to the std::array but because the resize maybe to the std::vector or std::deque...
We do not know what exactly is behind the MQL5 array. Therefore the question.

I think you can't compare C++(low-level) and MQL5(interpreted high-level). There is only syntactic similarity nothing more.

Sure you can in C++, resizing an array is just a matter of allocating the new memory, copy the contents to the new array,
deallocate the old array and return the address of the new array.

The fact the MQL is doing a single function call, is mere convenience.

Again, don't confuse std::array(which is a container) with array, they are not the same thing in C++.
Reason: