MetaTrade 4 Array++ BUG er language redefining over the years.

 

If you declare an array after extern and before start() with a cell limit of 5 like int Array[5] ; and then use ++ on them the last Array Cell [5] does not update!

To find this I wrote something like the following

int Array[5] ;

Start()
static int tick ;
tick++
if (tick>3) return(0) ;

Print("Next tick");

for(int i=0;i<=5;i++)
{
Print("Before++ Array[i]= ",Array[i]) ;
Array[i]++; 
Print("After++ Array[i]= ",Array[i]) ;
}

return(0) ;

Only the last integer Array value does not update with ++

 
If you have an array of 5 items, then only 5 items are incremented.
 

In other words ... the index should vary from 0 to 4, not 0 to 5

You have 0 to 5, which is 6 of them

 

I have an array ot 6 itme (trade types) and took into account that the array starts from 0 in making the array size.

I was looking for the answer to why sum of counted ordertypes was not equal to OrdersTotal

I think this is why I think x=x+1 is safer then x++

and the other habit of leave a little room at the end.

 
Ickyrus:
I have an array ot 6 itme (trade types) and took into account that the array starts from 0 in making the array size.
int Array[5] ;
You have an arrray of FIVE
for(int i=0;i<=5;i++)
but are indexing 0 .. 5 One past the end of 0 .. 4
I think this is why I think x=x+1 is safer then x++
Those are ABSOLUTELY IDENTICAL by definition. Safer is meaningless.
 

If I define

int x[5] ;

Is it or is it not reasonable to expect

x[5]++ ;

to increment by 1?

That is all that matters here. It has nothing to do with counting the number of items I wish to use in a program that is a seperate, insigificant and silly distraction from the above question. It is clear that when we count we start from 1 when we Name the places to save values in the names start at 0. This makes things easier internally. The effect for us is to shift the object count down numerically down by 1 as we use an infinite set of Names that start at 0 and then the names carry on in a familiar sequence. A memory location is a name and in that location a number is held and a program uses implicit knowledge to assign meaning to a number held in a named memory location.

Compilers are supposed to be user friendly not user confusing.

 

x[5] does not exists, you have only x[0] to x[4] available.

This is not a compiler error, this is a runtime error. Java would output a index out of bounds exception during execution.

I think you misunderstood the int var[5]; expression. it allocates an array of lenght 5, starting from 0 to 4

 

@Ickyrus -- "Compilers are supposed to be user friendly not user confusing."

Not so! Compilers are purely to translate various source-codes into a form more easily machine-processed in designated operating environments. It is the syntax and constructs for particular languages that are supposed to be user-friendly. To function correctly, a compiler must unambiguously interpret the specifications for the relevant language. For a programmer to function correctly, he must do the same.

 

Ok so in nearly every other language the difinition of declaring an Array the number inside [] is the highest index value you can use.

but in MetaTrader 'C' the definition is changed to mean the number of elements you wish to create and leave the range bound type checking to the programmer.

Not easy to change from one definition in one language to a diferent definition in another.

 
Ickyrus:

Ok so in nearly every other language the difinition of declaring an Array the number inside [] is the highest index value you can use.

Really? which language? Java C, C++, Visual Basic not.
 
zzuegg:
Really? which language? Java C, C++, Visual Basic not.

All these Languages were created AFTER Fortran, Cobol, Origional PDP-8 BASIC, Modula II, Pascal.

Reason: