You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Reserved size reflects the number of indexes or total number of elements?
The reserve size, just as the name indicates, is to reserve extra RAM space (that will not be used and remain reserved) just in the case you will need to expand your actual array size. It helps reduce or prevent memory fragmentation. When you resize the array to a larger size it will make use of the extra reserved space without actually redoing an allocation, as long as it does not surpass the reserved space.
Think of it has having a party at a restaurant where you are not sure of the number of invitees. You send out 20 invites, but don't know how many will come, but predict that only 15 will arrive. So you request seating for 15 people (size), and reserve an extra 5 seats, in case more is needed.
The reserve size, just as the name indicates, is to reserve extra RAM space (that will not be used and remain reserved) just in the case you will need to expand your actual array size. It helps reduce or prevent memory fragmentation. When you resize the array to a larger size it will make use of the extra reserved space without actually redoing an allocation, as long as it does not surpass the reserved space.
Think of it has having a party at a restaurant where you are not sure of the number of invitees. You send out 20 invites, but don't know how many will come, but predict that only 15 will arrive. So you request seating for 15 people (size), and reserve an extra 5 seats, in case more is needed.
Hello. you resize to a larger size, it will use the extra reserved space without actually repeating the allocation, as long as it doesn't increase the reserved space. do I understand everything correctly? Thank you
The reserve size, just as the name indicates, is to reserve extra RAM space (that will not be used and remain reserved) just in the case you will need to expand your actual array size. It helps reduce or prevent memory fragmentation. When you resize the array to a larger size it will make use of the extra reserved space without actually redoing an allocation, as long as it does not surpass the reserved space.
Think of it has having a party at a restaurant where you are not sure of the number of invitees. You send out 20 invites, but don't know how many will come, but predict that only 15 will arrive. So you request seating for 15 people (size), and reserve an extra 5 seats, in case more is needed.
What is the unit of reserve size, it is memory(bit, byte etc), it is number of elements or number of indexes in the array. It is important to properly calculate this value.
Obviously it is the same units as "size"! And if your new size is more than was previously available in the reserve, then more is allocated.
But you keep ignoring what I have already explained and that is to calculate a maximum (and store the array size in the file) and pre-allocate based on that.
Hi Samuel, thank you for relevant comment.
I would like to know in which cases I need to use reserve size when calling ArrayResize. And how actually determine this size.
in my particular case I am using ArrayResize in the loop to read structure dynamic array from bin file, with each iteration increasing the size by one. I suspect that in my case I should not use the reserve size.
Use reserve when you are sure that will need resize it again in a brief time and u don't know exactly amount of data which will be assign. If u know exactly amout of data don't use reserve, resize it for the size required once and don't need resize it 1000 times in loop.
In the function ArrayResize, the new size the amount of elements in the array, and the reserve the amount of element for which memory is allocated. Let't suppose u do ArrayResize(array,10,20), then the array size is 10 and the allocated memory if for 30 elements, i mean the new size + reserve size. It means that memory will be allocated again when array size is 30.
Just make sure to not use a reserve 1000 if will use 10 or 50 for example.
Is it clear?
Here an example of the reserve parameter usage effect
And this is the result.
Here an example of the reserve parameter usage effect
And this is the result.
Thank you for detailed explanation.
Is there any function to get the allocated memory for specific array?
You should start reading the documentation more: ArraySize() - The function returns the number of elements of a selected array.
During hundreds of test run recently I faced array out of range in a function immedaitely after calling ArrayResize.
So the quesion is, is it necessary to put a control to avoid this.
I am using the mqh file that lets you know about a similar issue right away.