Script execution multiple times shows old values

 

Hi,

i often test mql behaviour in a script. In this case I noticed that an array which is filled, is not initialized (and keeps old values) if executing the same script again. How can I force to have a clean environment when starting the script again?

void OnStart() {
    int aSize = 6;
    int a1[];
    ArrayResize(a1, aSize);
    // ArrayFill(a1, 0, aSize, 0); // fills array to remove old values


    for(int i = 0; i < aSize; i++) {
        a1[i] = i;
        Print("set i: " + i);
    }

    ArrayPrint(a1);
}

This script filles the array with 0 1 2 3 4 5. Now changing the loop:

for(int i = 0; i < aSize - 4; i++) {

I would expect the array values 0 1 0 0 0 0, but Arrayprint(a1) shows 0 1 2 3 4 5. This is sometimes a bit confusing. Seems that the old values are still in memory but why after changing, compiling and executing the script again? Is there a possibility to reset the data without restarting the mt5?


Best regards

trado19

 
trado19:

Hi,

i often test mql behaviour in a script. In this case I noticed that an array which is filled, is not initialized (and keeps old values) if executing the same script again. How can I force to have a clean environment when starting the script again?

This script filles the array with 0 1 2 3 4 5. Now changing the loop:

I would expect the array values 0 1 0 0 0 0, but Arrayprint(a1) shows 0 1 2 3 4 5. This is sometimes a bit confusing. Seems that the old values are still in memory but why after changing, compiling and executing the script again? Is there a possibility to reset the data without restarting the mt5?


Best regards

trado19

You are always responsible for clearing any memory you want to use, before you use it.

ArrayInitialize, ArrayFill, and ZeroMemory as well as constructors are the tools to be used.
 
i am sure that ArrayInitialise will fill the array with the first values. But I do not know how to remove the array upon completion of the script or deinitialisation of it. I will watch this thread!
 
Dominik Egert #:
You are always responsible for clearing any memory you want to use, before you use it.

ArrayInitialize, ArrayFill, and ZeroMemory as well as constructors are the tools to be used.

yes, but is the array still in memory with just the values changed to 0? or is the array removed from memory?

 
Michael Charles Schefe #:

yes, but is the array still in memory with just the values changed to 0? or is the array removed from memory?

Memory is managed by the OS. The OS gives you an area, marks this area as "in use" and makes sure you can use it. The OS doesn't care about the content, except for the area being "non-executable".

After you are done with the area, iE you explicitly release it, or your program terminates, the OS will mark the area as free. The OS still doesn't care about its content.

That's why encryption software usually overwrites its used memory before releasing it back to the OS.
 
Dominik Egert #:
Memory is managed by the OS. The OS gives you an area, marks this area as "in use" and makes sure you can use it. The OS doesn't care about the content, except for the area being "non-executable".

After you are done with the area, iE you explicitly release it, or your program terminates, the OS will mark the area as free. The OS still doesn't care about its content.

That's why encryption software usually overwrites its used memory before releasing it back to the OS.

what you said is exactly how I understood it to be, however, the op has described how their script reported the array contents after it was finished and the script/program was terminated.

And I can attest to the same unexpected behaviour as the op described.
 
Michael Charles Schefe #:

what you said is exactly how I understood it to be, however, the op has described how their script reported the array contents after it was finished and the script/program was terminated.

And I can attest to the same unexpected behaviour as the op described.
If I haven't fully misunderstood, it is not "unexpected" but actually "expected", and the way it is supposed to be.

Memory content is managed only by custom code.

EDIT:

My explanation did not cover all aspects of memory allocation. To get into how it works, you need to understand the underlying concepts of alloc, malloc, calloc.

A more in detail explanation can be found in this thread:


 

if you say it is expected, then, I will just believe you :D.