Possible small bug in compiler in latest versions

 

Hi!

Since one of the latest versions (I won't be able to remember which, but I'm talking about last 1 month or so), I started to get the error "possible use of unitialized variable" in a code where that doesn't make sense (AFAICS):


The code is as follows:

MqlDateTime currDTS;
      
TimeToStruct2100(currDT,currDTS);

const int currDay = currDTS.day;

MqlDateTime dtS;

for (int aaa = 0; aaa < 290; ++aaa)
{
        TimeToStruct2100(pastDTs[aaa],dtS);
                                                
        if (dtS.day != currDay)
                break;
}

So the compiler is saying that dtS may be used uninitialized, but that can't be the case: inside the for loop, at least one call to TimeToStruct2100 is going to happen (since 0 will always be < 290) where the structure will receive data:

//https://www.mql5.com/en/forum/393227/page259
bool TimeToStruct2100(datetime time, MqlDateTime& dt_struct) 
{
   static const int dm[13] = {0, 0, 31, 59, 90, 120, 151, 181, 212,243, 273, 304, 334};
   static int last_days = 0;
   static int last_result_m = 0;
   static int last_result_y = 0;
   static int last_result_yd = 0;
   static int last_result_d = 0;
   
   int days = (int)(time / (60 * 60 * 24));
   
   if (last_days!=days) 
   {
      last_days = days;
      last_result_y = ((days << 2) + 2) / 1461;
      last_result_yd = days - ((last_result_y * 1461 + 1) >> 2);
      int isleap = ((last_result_y & 3) == 2);
      int leapadj = ((last_result_yd < (59 + isleap)) ? 0 : (2 - isleap));
      last_result_m = ((((last_result_yd + leapadj) * 12) + 373) / 367);
      last_result_d = last_result_yd-dm[last_result_m]+1 -((last_result_yd>59)?isleap:0);
   }

   int HH  = (int)((time / 3600) % 24);
   int MM  = (int)((time / 60) % 60);
   int SS  = (int)(time % 60);
   int dow = (days + 4) % 7;

   dt_struct.year           = last_result_y+1970;
   dt_struct.mon            = last_result_m;
   dt_struct.day            = last_result_d;
   dt_struct.hour           = HH;
   dt_struct.min            = MM;
   dt_struct.sec            = SS;
   dt_struct.day_of_week    = dow;
   dt_struct.day_of_year    = last_result_yd;
   
   return true;
}

Am I missing something or is the compiler really getting a false positive? In any case, the problem is easy to fix:

TimeToStruct2100(pastDTs[0],dtS);

for (int aaa = 1; aaa < 290; ++aaa)
{
        if (dtS.day != currDay)
                break;
                                                
        TimeToStruct2100(pastDTs[aaa],dtS);
}

But I suppose MetaQuotes should be informed of the issue in case there is actually a bug, so here I am. Any thoughts?

 
Martin BittencourtSo the compiler is saying that dtS may be used uninitialized, but that can't be the case: inside the for loop, at least one call to TimeToStruct2100 is going to happen 
You passed an uninitialized variable to TimeToStruct. The compiler doesn't know that the function initializes the struct, not uses it. Initialize it:
MqlDateTime dtS={0};
 
William Roeder #:
The compiler doesn't know that the function initializes the struct

Hi! Thanks for the reply!

Well, that would explain the problem partially. My question is: in that case, why the compiler doesn't complain about the previous structure, currDTS, since it's also initialized whitin the same function?