ArrayResize() question

 

I thought ArrayResize() would also shrink the array, if a smaller length is given.

I did:

int a[] = {1,2,3,4,5,6,7,8,9,10};
ArrayResize(a,2);
printf("len %i", ArraySize(a)); 

//the result is not 2

 

Is this normal? or is it a bug?

I don't know if I should report it to service desk 

 

I found ArrayFree(), and it's even weirder now:

int a[] = {0, 1, 2, 3, 4, 6, 6, 8, 9, 10, 10, 10, 10, 10, 10, 10,10,10, 12, 16, 25, 36, 49};
ArrayFree(a);
ArrayResize(a, 2);
printf("len a %i", ArraySize(a));

 The result is not 2, again.

I don't get it, it appears to be a bug, or I am missing something extremely basic. 

 
Mihai Ionescu:

I found ArrayFree(), and it's even weirder now:

 The result is not 2, again.

I don't get it, it appears to be a bug, or I am missing something extremely basic. 

The array is considered as static, you can't resize or free it.
 
Alain Verleyen:
The array is considered as static, you can't resize or free it.

I don't understand why it's considered static.

There's no static keyword there, just initialization with some values.

This is not right from my point of view. 

It's like saying:

int i = 5; //should be static

 feels wrong

 

Static Arrays

When all significant array dimensions are explicitly specified, the compiler pre-allocates the necessary memory size. Such an array is called static.

feels right.

ArrayIsDynamic(....





 
Marco vd Heijden:

Static Arrays

When all significant array dimensions are explicitly specified, the compiler pre-allocates the necessary memory size. Such an array is called static.

feels right.





The array declared above does not specify dimension.

It's just initialized.

With dimension specified, I guess it's ok to forbid resizing.

 

I didn't understand the part with dynamic array in docs:

"[...] Such an array is called static. Nevertheless, the compiler allocates additional memory for the object of a dynamic array, which (object) is associated with the pre-allocated static buffer (memory part for storing the array).

 
Alain Verleyen:

It's as you wish.

Anyway Time[], High[], Low[], Open[],Close[] are static arrays.

Don't they grow with incoming candlesticks?

Then dimension increases with each candle, although arrays are static (by definition above)? 

 
//+------------------------------------------------------------------+

int a[] = {1,2,3,4,5,6,7,8,9,10};
  
  if(ArrayIsDynamic(a)==1)
   {
    Print(" Array a is a dynamic array");
   }
  else if(ArrayIsDynamic(a)==0)
   {
    Print(" Array a is not a dynamic array");
   } 

RESULT:  Array a is not a dynamic array

//+------------------------------------------------------------------+

int a[];
  
  if(ArrayIsDynamic(a)==1)
   {
    Print(" Array a is a dynamic array");
   }
  else if(ArrayIsDynamic(a)==0)
   {
    Print(" Array a is not a dynamic array");
   } 

RESULT:  Array a is a dynamic array

//+------------------------------------------------------------------+
 
Marco vd Heijden:

Well, then, in first example, the array should be dynamic, that's my point. MT5 wants to make the array static, but it's not ok from my point of view. It's not intuitive.

Initialization should initialize values (and automatically do an ArrayResize() ). That's why it's called initialization.

a[] should be dynamic nevertheless.

a[n] should be static. 

 

I removed some of my post as I confused with another thread.

Mihai Ionescu:

Well, then, in first example, the array should be dynamic, that's my point. MT5 wants to make the array static, but it's not ok from my point of view. It's not intuitive.

Initialization should initialize values (and automatically do an ArrayResize() ). That's why it's called initialization.

a[] should be dynamic nevertheless.

a[n] should be static.

It works as it is. You can't expect the language to work as you wish, YOU have to follow the language rules.

From documentation :

If the size of the initialized array is not specified, it is determined by a compiler, based on the size of the initialization sequence.


 

No in the first example it should be static because you specify it will only be dynamic if it can not determine type and fill in or calculate elements.

Once you specify:

= {1,2,3,4,5,6,7,8,9,10};

It makes all the difference.

If you want dynamic first declare open or endless situation:

int a[];

then it can not know or calculate type and size so it HAS to assume dynamic.

then specify:

ArrayResize(....);

Resize, and then fill.

It's logical operations not intuitive.

for me it is only logically, once you specify:

= {1,2,3,4,5,6,7,8,9,10};

it's way of managing resources here.

if this wasn't so, how would it have to calculate resources? it would not have a clue and go funky.

Reason: