Compiler Error: 'ArrayRange' - initialization expected ?!?!?!

 

In my EA I have an Array called 'TickData' which saves each incoming tick information.

I also built custom functions that work and perform calculations using information from that array.

In each function I use the internal function 'ArrayRange(TickData,0)' to get the amount of elements in my array before proceeding with calculations.

I wanted to do a very simple thing: instead of calling 'ArrayRange(TickData,0)' from within each function, I thought I'd only call it once by assigning it's value to a global variable:

int arSize = ArrayRange(TickData,0);

...and then allowing the functions to use that value, thus saving execution time and optimizing my EA.

However, as soon as I did that I got a compilation error saying:

'ArrayRange' - initialization expected

Pointing me to this portion of the code:

double TickData[][5];
int arSize = ArrayRange(TickData,0); <--

But what is the problem here??

If I copy-paste this code into a script it will compile and work problem free:

int start()
  {
//----

double TickData[][5];
int arSize = ArrayRange(TickData,0);

Alert(arSize);

//----
   return(0);
  }

The result of Alert would be 0, as in there are 0 elements in the first dimension.

Obviously there is some conflict between this and and the rest of the code in my EA as opposed to the script, but I have no idea what it could possibly be! :\

Does anyone have any ideas?

Thanks!!

nanquan

 
double TickData[][5];
int arSize = ArrayRange(TickData,0);

TickData is a pointer and has no size yet. Once you allocate some memory for storage then the ArrayRange will work.

double TickData[1][5];
int arSize = ArrayRange(TickData,0);

will work.

 
But then why does the exact same code work if you run it as a script?
 
nanquan:
But then why does the exact same code work if you run it as a script?

I assume that you do an ArrayResize somewhere to make the pointer actually point to a memory location.

If you are running the same code sequence then it is possibly a compiler anomaly. I am not sure what value you are hoping for if you test a null pointer. It would be best not to test a null pointer like this, especially when you know it will be null until you do something!

 

Well logically the value of

double TickData[][5];
int arSize = ArrayRange(TickData,0);

should be ZERO. Since null means there are 0 elements in the 1st dimension.

I actually found a workaround to this problem by calling ArrayRange() from within one of the special functions for the 1st time, and then assigning it's value to global variable arSize.

So apparently this means that the global variables initialize with the 1st run of one of the special functions init(), start(), deinit().

So when I called for ArrayRange() before any of the special functions then it threw that compilation error.