initializing non-static variables inside init()

 

Suppose I have the following code:

//... global scope 
int var;

int init()
{
//...
   var = 3;
//...
}

int start()
{
//...
   Print("var = ",var);
//...
}

the output each time start() runs is "var = 3"

why?? If I did not declare var as static the memory partition where 'var' is stored should be wiped clean each run and since init() only gets called during the first run, var should not be set to 3 each consecutive run.

The output I would expect from the code above is "var = 3" only once, then "var = 0" every other time.

I would think that in order to have var = 3 come up each run, one would have to declare var as static.

Can anyone explain to me what Im missing?

Thanks.

 
supertrade:

Suppose I have the following code:

the output each time start() runs is "var = 3"

why?? If I did not declare var as static the memory partition where 'var' is stored should be wiped clean each run and since init() only gets called during the first run, var should not be set to 3 each consecutive run.

The output I would expect from the code above is "var = 3" only once, then "var = 0" every other time.

I would think that in order to have var = 3 come up each run, one would have to declare var as static.

Can anyone explain to me what Im missing?

Thanks.


The behavior you are seeing is correct. You have declared var globally.

If you declared var in the init() fucntion it would not be visible in the start() function, static or not.

If you declared var as static in the start() function, it would only be visible to start() and to functions called from start(), yet it would retain its value from one call to start() to the next.

 

I understand the concept of scope. The thing Im having trouble understanding is why a non-static variable retains its value when that value is declared globally and defined only inside init().

It seems as if from the scope of start(),

int var;

int init() { var=3; }

..is the same as..

int var=3;

int init() { ... }

 
Because global variables are static variables ... thats how I understand it.
 

Global variables aren't technically static. It won't likely lead you astray to think of them that way, though. Global variables have file scope.

int var;

int init() { var=3; }

..is the same as..

int var=3;

int init() { ... }

Yes, these are basically the same. MQL4 is no different from C or C++ as far as scope is concerned.

 
Anomalous:

Global variables aren't technically static. It won't likely lead you astray to think of them that way, though. Global variables have file scope.

Yes, these are basically the same. MQL4 is no different from C or C++ as far as scope is concerned.


I see. Until now I was not aware of global variable having (essentially?) the same behavior as static variables. I will look further into this connection and the concept of a 'file scope' now. Thank you.

So would it be redundant to declare a static, global variable?

 
supertrade:

I see. Until now I was not aware of global variable having (essentially?) the same behavior as static variables. I will look further into this connection and the concept of a 'file scope' now. Thank you.

Wikipedia explains it more clearly than I have. See here.
 

Just to muddy the waters a little more.

int var=3;
start(){ var++; }
and
int var;
init(){ var=3; }
start(){ var++; }
Are not the same. In the later, every parameter change and chart change (TF,pair) will reset var back to 3 via init. In the former it will NOT be reset since the EA is not reloaded.
Reason: