Array Initialization Question.

 

Hi,

If I declare an array of size 1, I can write and read values much beyond 1. How is this possible? Is it resizing everytime?

Also, I noticed that I either have to initialize the array, or provide a size during declaration for it to work properly. See the code below that does none of these, and fails to give increments.

So the question is, how do the MetaTrader indicators work that neither provide initialization values, nor size of the indicator arrays?

int var[];


int start(){


var[0]=var[0]+1;

Alert("Var is ",var[0]);

}

This gives:

Var is 0

Var is 0

Var is 0

...

If change the declaration to

int var={0};

This gives:

Var is 0

Var is 1

Var is 2

as desired.

So, how do the indicators work with arrays when they don't have the values at declaration?

Thanks,

JForex.

 

The declaration of int var[] is called dynamic array, you have to use ArrayResize() to specify its dimension before use. If you want it a fixed size just declare it as int var[1] or whatever. Otherwise it will only give you a zero in value.

P.S. when the index of an array out of boundary, mt4 will only give you a zero in return. I didn't remember what error will generate if you do that, but you can check with GetLastError() too see is your last excution of code have out of boundary or not.

Here I have found the information about indexing of an array, please have a look https://docs.mql4.com/basis/operations/other

 
brother3th wrote >>

The declaration of int var[] is called dynamic array, you have to use ArrayResize() to specify its dimension before use. If you want it a fixed size just declare it as int var[1] or whatever. Otherwise it will only give you a zero in value.

P.S. when the index of an array out of boundary, mt4 will only give you a zero in return. I didn't remember what error will generate if you do that, but you can check with GetLastError() too see is your last excution of code have out of boundary or not.

you missed the point.

see the point below:

"So the question is, how do the MetaTrader indicators work that neither provide initialization values, nor size of the indicator arrays?"

JFoxex has a very good question. what i think is just when you use set buffers to the indicator function, it will work.

 
caijs wrote >>

you missed the point.

see the point below:

"So the question is, how do the MetaTrader indicators work that neither provide initialization values, nor size of the indicator arrays?"

JFoxex has a very good question. what i think is just when you use set buffers to the indicator function, it will work.

Yes, I missed the point. But the set buffers function only work in indicator script and not work in EA script. I have read some articles about how to embed indicator into EA on this forum. It use the combination of ArrayResize() and ArraySetAsSeries() to emulate the indicator array in EA. They don't have any function can do something like indicator array directly. Sometime mql4 just make people disappointed. :(

 

Thanks for the responses.

Apparently, its just unclear how the indicators work without size/ initialization information.

As caijs said, its perhaps getting done in the SetIndexBuffer(int,double&) function. So I will take that for an answer.

Thanks,

JForex.

Reason: