Errors, bugs, questions - page 417

 
joo:

yes, if something needs to be returned in return().

or at least so that the compiler is sure that the function will return something:

this is true not only for
switch

but in general, for all functions except void

Try to compile this code:

//+----------------------------------------------------------------------------+
//|                                                                  Scale.mqh |
//|                                             Copyright © 2010, JQS aka Joo. |
//|                                           http://www.mql4.com/ru/users/joo |
//|                                        https://www.mql5.com/ru/users/joo |
//——————————————————————————————————————————————————————————————————————————————
double Scale(double In,double InMIN,double InMAX,double OutMIN,double OutMAX)
{
  if (OutMIN==OutMAX)
    return(OutMIN);
  if (InMIN==InMAX)
    return((OutMIN+OutMAX)/2.0);
  else
  {
    if (In<InMIN)
      return(OutMIN);
    if (In>InMAX)
      return(OutMAX);
    //return(((In-InMIN)*(OutMAX-OutMIN)/(InMAX-InMIN))+OutMIN);
  }
}
//——————————————————————————————————————————————————————————————————————————————

it doesn't compile.

Now uncompile it:

//return(((In-InMIN)*(OutMAX-OutMIN)/(InMAX-InMIN))+OutMIN);

and a miracle will happen! :)

 
Yedelkin:

According to the Handbook, a bool is a special type other than integer... That's why I deleted my incorrect statement. Although I'm not going to argue - I'm not an expert.

I meant not case labels, but just enumerations (considering bool type as the smallest of them). Here is an example with the same compilation error:

So I'll repeat my question concerning this example: do you mean that the compiler doesn't take into account the list of values from the Triple enumeration and their total number? I have all values from the enumeration used in the switch operator.

As I understood the compiler (developers) are reinsuring. The point is that if a variable is not explicitly initialized it will get false/0 value depending on its type (the second refers to enumerations).

But if we get a value outside the range of possible variants (in example with Triple it can be any value outside the range -1 - +1) in execution without devoultive result there will be serious problems.

 
Interesting:

What should the function return if the defaulter is excluded?

I think the last value from the ENUM_CHART_MODE enumeration. I will check it now.

...mmm, it did not work. Prints the following: ChartMode=ENUM_CHART_MODE::-1
 
Interesting:

As I understand it, the compiler (developers) are reinsuring. The point is that if a variable is not explicitly initialized it will get false/0 value depending on its type (the second refers to enumerations).

But if we get a value outside the range of possible variants (in an example with Triple it can be any value outside the range -1 - +1), we will have a serious problem when executing the variable without any devout result.

That's not exactly the point. Look at my previous post.
 
joo:
That's not exactly the point. Look at my previous post.

Yes, the compiler has to make sure it returns something. I think this is a reasonable reassurance (in particular, having a default on switch processing in the example).

It's another matter if you don't need to return a value.

 
Yedelkin:

I think the last value is from the ENUM_CHART_MODE enumeration. I'll check it now.

...Yeah, it didn't work. Prints the following: ChartMode=ENUM_CHART_MODE::-1

If you want to get deep into all such intricacies, read Björn Straustrup, C++.

To be honest, I haven't even really read the MQL5 documentation - I'm just writing as in C++. The developers follow rather precisely the standards of this language.

 

Good afternoon!

build 466.

I start, as soon as the connection appears and a few kilobytes are downloaded, the terminal closes. I disconnect the internet - it won't close.

I am attaching the file from /logs/Crash/ directory.

Is there a solution to this problem?

Thanks

)) is not attached. Here is the text:

Time : 2011.06.16 10:28 (0:00:11)

Program : Client Terminal

Version : 500.466 (09 Jun 2011)

Revision : 32925

OS : Windows 7 Professional Service Pack 1 (Build 7601)

Processors : 2 x AMD Athlon 64 X2 Dual Core Processor 5000+

Memory : 911 free of 1983 Mb

Virtual : 1815 free of 2047 Mb

CrashMD5 : 2219A3BB7215B179256A7E41D40BD511

Exception : C0000094 at 007B41B4 NA to 00000000


Modules : 00400000 00B96000 terminal.exe (5.0.0.466)

: 6FDC0000 00027000 wlidnsp.dll (7.250.4225.0)


007B41A0:00014 [007B41B4] #22663 (terminal.exe)

774D58FC:00C74 [774D6570] strcspn (ntdll.dll)

774D58FC:00CAA [774D65A6] strcspn (ntdll.dll)

74A5DC14:000EC [74A5DD00] func_0x74A5DC14 (dbghelp.dll)

74A5E10D:0016E [74A5E27B] SymGetLineFromAddr64 (dbghelp.dll)

74A5F73A:0085A [74A5FF94] func_0x74A5F73A (dbghelp.dll)

74A6189C:000D2 [74A6196E] func_0x74A6189C (dbghelp.dll)

74A5F73A:00A54 [74A6018E] func_0x74A5F73A (dbghelp.dll)

74A5DC14:000EC [74A5DD00] func_0x74A5DC14 (dbghelp.dll)

74A5E10D:0016E [74A5E27B] SymGetLineFromAddr64 (dbghelp.dll)

74A5F73A:0085A [74A5FF94] func_0x74A5F73A (dbghelp.dll)

74A6189C:000D2 [74A6196E] func_0x74A6189C (dbghelp.dll)

74A5F73A:00A54 [74A6018E] func_0x74A5F73A (dbghelp.dll)

774D68C7:000E0 [774D69A7] RtlLogStackBackTrace (ntdll.dll)

774D58FC:004D7 [774D5DD3] strcspn (ntdll.dll)


Registers : EAX=000000000000 EIP=007B41B4 EFLGS=00010246 ES=0023

: EBX=000000000000 ESP=0012E2CC EBP=0012E320 FS=003b

: ECX=00000000 ESI=04C74C48 CS=001b GS=0000

: EDX=000000000000 EDI=00000007 DS=0023 SS=0023

 

Continuing the theme of using switch operator in functions that return values. Yesterday an intermediate conclusion was obtained and confirmed thatusing default label becomes mandatory when using enumeration+switch. But here is an example when this conclusion is disproved:

enum Triple
  {
   err=-1,
   no = 0,
   hay= 1
  };
Triple triple_var1,triple_var2;
Triple Test(void)
  {
   switch(triple_var1)
     {
      case  err: return(err);
      case   no: return(no);
      case  hay: return(hay);
      default:
         switch(triple_var2)
           {
            case  err: return(err);
            case   no: return(no);
            case  hay: return(hay);
           }
     }
  }
void OnStart()
  {
   Test();
  }
Here the switch operator is applied twice and the default label is excluded when applying it again (2nd level). Same operator and the same compiler, but it works. Assuming that the compiler takes into account possibility of finding any rubbish in triple_var1 and triple_var2 variables and simultaneously does not consider the list of values in Triple enumeration and their number, why won't the compiler report an error on the 2nd level of switch operator? Our intermediate conclusions/assumptions were wrong or the compiler limits itself to checking operators only "on the 1st level"? In particular, if we comment out the label/tags from switch(triple_var2), there are still no error messages, although Test() function is not of void type.

A similar result is obtained if the switch(triple_var2) operator (without the default label) is inserted after any case label in the switch(triple_var1) operator.

 
Yedelkin:

Continuing the theme of using switch operator in functions that return values. Yesterday, an intermediate conclusion was obtained and confirmed thatusing default label becomes mandatory when using enumeration+switch. But here is an example where this conclusion is refuted:

Here the switch operator is applied twice, and when it is repeatedly applied (2nd level), the default label is excluded. It is the same operator and the same compiler but it works. Assuming that the compiler takes into account probable presence of trash in the triple_var1 and triple_var2 variables and simultaneously does not consider the list of values in Triple enumeration and their number, why won't the compiler report an error on the 2nd level of switch operator usage? Our intermediate conclusions/assumptions were wrong or the compiler limits itself to checking operators only "on the 1st level"? In particular, if we comment out the switch(triple_var2) label/tags, no error messages will still appear, although the Test() function is not of void type.

This is our flaw, thanks for finding it, we'll fix it. There will be an error.
Checking switch to cover all possible values in case is beyond compilation.
It's interesting as a MQL5 "feature", we will think about what to do.
 
The error has been corrected.
"The switch checker chip has been discussed, it is not possible to implement a valid/correct control.
The value of the switch expression can be anything, for example:

enum EV { v1, v2, };

string Test(void)
  {
   switch(EV(3))
     {
      case v1: return("v1");
      case v2: return("v2");
     }
   return("oops");
  }
  
void OnStart()
  {
   Print(Test());
  }
Reason: