In MT4 if we use an un-initialized local auto non-static variable, can the initial value be sometimes not zero ?

 
Hi Slawa,

In MT4 will a local non-static variable (that has not been initializated explicitly using init() function) "always" start off as having a value of zero? Can I always **safely rely** on all of my non-static local variables starting off at zero?

The second question I had is: If I assign a value, using an operation, to all of my local non-static variables within the start function block of code, will the last assigned value for all of my non-static local variables be remembered by my computer, after the start function or endless loop block of code keeps on looping?

All of my MQL4 code is within the start function. I do not properly understand or use the init() or deinit() functions in my expert.

The expert advisor code I am using is set out below. Basically, I want to send me an Alert if the price quotes stop moving for 5 minutes (say due to broker server going down or some other data feed problem). I need Slawa to assure me that the "count" non-static local variable (in my code below) will "always" start at zero, and the last assigned value will be remembered and not lost each time the endless loop loops?


int start()
{
 
 
  while( IsStopped()==false )  // endless loop
  { 
   
   
      int count;
  
      
      // if server clock stops (price freezes) for 
      // more than 5 mins then alert me.
      if( CurTime() - LocalTime() == 300 ) 
       
         {
        // counter
         count=count+1;
             
              if (count<2)
                 {
                 Alert("Price Quotes feed has stopped....PROBLEM !!!" );
                  }
          }

  }

}





Thanks in advance.


Regards

RJF


 
int start()
{
 
  int count=33;
 
  while( IsStopped()==false )  // endless loop
  { 
      // if server clock stops (price freezes) for 
      // more than 5 mins then alert me.
      if( CurTime() - LocalTime() == 300 ) 
         {
        // counter
         count=count+1;
              if (count<2)
                 {
                 Alert("Price Quotes feed has stopped....PROBLEM !!!" );
                  }
          }
  }
}



count variable will be initialized with 33 every start function call

 
int start()
{
 
  int count=33;
 
  while( IsStopped()==false )  // endless loop
  { 
      // if server clock stops (price freezes) for 
      // more than 5 mins then alert me.
      if( CurTime() - LocalTime() == 300 ) 
         {
        // counter
         count=count+1;
              if (count<2)
                 {
                 Alert("Price Quotes feed has stopped....PROBLEM !!!" );
                  }
          }
  }
}



count variable will be initialized with 33 every start function call



Thanks Slawa. One other question: If we do not explicitly set count variable to zero before the start function, will (non-static) count variable always start at zero ?

Slawa, the reason I ask you this question is that in C++ Programming language un-initialized non-static variables do not "always" start off at zero before they are assigned a value. ie not always reliable.
 
If we do not initialize count variable explicitly, will (non-static) count variable always start at zero ?

its value is undefined
 
If we do not initialize count variable explicitly, will (non-static) count variable always start at zero ?

its value is undefined


Hi Slawa. Thanks for this. You say value for count variable is initially undefined. However, when you run the expert advisor program provided below you will see in journal (under experts tab) that value for count variable starts off at zero (immediately after you run the expert advisor). So are you saying in MT4 all undefined values for variables start off at zero, even though really they are undefined to start with? I say this as zero is not the same as undefined in mathematics ? Could you please run the expert advisor code set out below and you will see that print value for count variable starts off at zero (and so is defined as zero initially).

Thanks Slawa. Just want to clear this up. Here is code (note printed initial value for count variable in journal. Initial value is defined as zero and therefore not un-defined). How do you explain this Slawa?


int start()
{
 
 
   while( IsStopped()==false )  // endless loop
   { 
   
   
      int count;
  
           Print( "Value of Count variable : ", count );
     
                count=count+1;
             
   }

}





Thanks in advance.

Regards

RJF



 
Here is code (note printed initial value for count variable in journal. Initial value is defined as zero and therefore not un-defined). How do you explain this Slawa?

There is just your goodluck. In other cases it may be wrong.

Defined are static variables, global scope variables (they are static too) and explicitly initialized variables.
 
Here is code (note printed initial value for count variable in journal. Initial value is defined as zero and therefore not un-defined). How do you explain this Slawa?

There is just your goodluck. In other cases it may be wrong.

Defined are static variables, global scope variables (they are static too) and explicitly initialized variables.


Thanks Slawa. This is what I thought. However, in MQL Reference under "initialization of variables" heading why does it say "Any variable can be initialized at its defining. Any variable is initialized with zero (0) if no other initial value is explicitly defined.". It says "any variable" is initialized with zero, not just static variables?? Is this correct?

Besides all of this, how should I program the following code, so that Count variable always safely starts at zero and then counts the number of times the endless loop loops. How do you recommend I program this, using static variable or other safe method, that does not depend on luck? Please see code below:

Thanks in advance.



int start()
{
 
 
   while( IsStopped()==false )  // endless loop
   { 
   
   
      int count;
  
           Print( "Value of Count variable : ", count );
     
                count=count+1;
             
   }

}

 
"initialized with zero" at beginning! When program just loaded.

If some local variable not initialized explicitly then in several steps You can get unexpected value
 
"initialized with zero" at beginning! When program just loaded.

If some local variable not initialized explicitly then in several steps You can get unexpected value



Hi Slawa. Thanks for your answer. I was thinking maybe I could program the loop counter like this:


int start()
{
 
 
   while( IsStopped()==false )  // endless loop
   { 
   
   
       static int count = 0;
  
           Print( "Value of Count variable : ", count );
     
                count=count+1;
             
   }

}




Is this correct to do it this way? As even though Count Variable is initialized to zero explicitly within the endless loop, it is only initialized "once" at the start and not each time the loop loops, as it is a static variable. Is it also ok to initialize the static variables within the start function?

Thanks again.

Regards

RJF



Reason: