Errors, bugs, questions - page 2046

 
Alexey Viktorov:

This initialization is possible. Because the 'a' variable is initialized with a constant, and the 'b' variable is initialized with a constant expression.

Here 'b' is initialized NOT by a constant expression. That's why it contradicts the rules described in the documentation.

The whole problem is that when astatic variable is initialized by a function, the initialization is "paused" and the function is executed. In the above example, there is a static variable in that function that hasn't been initialized yet. Hence, a variable initialized by a function takes a different value.

But how can initialization be paused? All the operations are executed in a strict order defined by language syntax. First, the function is executed, and then the returned value of this function is passed to the constructor of our variable - this is called initialization. But the initialization operation is blatantly ignored by the compiler in this case, and the code just keeps compiling as if nothing had happened. This is unacceptable. It is the same as if you, for instance, declared the following array int a[]= { f(), g(), h() }; and it would compile, but of course without initializing anything.

 
A100:

In 32-bit terminal an error on any OPTIMIZE

Real. Checked in MT4, there are indeed errors in any way. And how people code something there on new builds
 
Alexey Navoykov:

'b' here is initialised by NOT a constant expression. So it is against the rules described in the documentation.

How can initialization be paused? All the operations are performed in a strict order defined by language syntax. First, the function is executed, and then the returned value of this function is passed to the constructor of our variable - this is called initialization. But the initialization operation is blatantly ignored by the compiler in this case, and the code just keeps compiling as if nothing had happened. This is unacceptable. It is the same as if you, for instance, declared the following arrayint a[]= { f(), g(), h() }; and it would compile, but of course without initializing anything.

If not by a constant expression, then by WHAT???

Alexey, it's easier if you take that code yourself and look through the initialization sequence in the debugger. Also, I didn't immediately notice that in this example

Forum on trading, automated trading systems and strategy testing

Bugs, bugs, questions

Alexey Navoykov, 2017.10.17 20:31

And here's more on the topic of variable initialization. If you follow what was mentioned in documentation, then you can't reference other global/static variables too. As it is not a constant expression:

int a= 1;
int b= a+1;  // Согласно документации, такая инициализация не возможна

void OnStart()
{
  Print(b);
}

However, for now it works. And I suppose many people have always used such constructs without realizing it. But it turns out that the developers may disable it at any moment. And like in the case with function initialization, these codes will still compile successfully, but they won't work correctly. So, the whole MQL code is a time bomb.


It's the global level variables, not the static ones located inside any function.

I don't have enough knowledge to explain it, and you are not attentive enough. "suspended" in inverted commas!!!

Don't confuse initialization of static and ordinary variables, all the more so for local variables.

 
Alexey Viktorov:

If not by a constant expression, then by what?

A non-constant expression.

Also, I didn't immediately notice The main point is that this example talks about global-level variables and not static ones located inside any function.

I don't have enough knowledge to explain it in a clear way, and you don't have the attention span...

Aren't you confusing something?

Moreover, global and static variables behave absolutely identically. Here is the documentation:

A global variable may be initialized only by a constant or a constant expression corresponding to its type.

I have to confess that it was as much of a revelation for me as for static variables. I don't know when it all appeared in the documentation or maybe it was earlier, but nobody paid attention to it, because everything actually worked as in C++, so no questions arose.

 
Alexey Navoykov:

A non-constant expression.

Aren't you confusing things?

Furthermore, global and static variables behave exactly the same. Here's something from the documentation:

I have to confess that this was as much of a revelation to me as it was with static variables. I don't know when it all appeared in the documentation and maybe it was earlier but nobody paid attention to it since everything actually worked in C++ and therefore no questions arose.

I think you are absolutely wrong. How can you talk about the identity of variables, if some are available from all the functions defined in the program and others only in the function in which they are declared.

And the difference between static and local variables is that static variables are initialized immediately after the global, and ordinary local (non-static) as the program executes, when the code gets to them.

 
Alexey Viktorov:

It seems to me that you are completely mistaken. How can you talk about variables being identical if some are accessible from all functions defined in the program, and others only in the function in which they are declared.

You were talking about the specific context of the discussion(variable initialisation), not in general.

 
After sending messages via full chat, the sent message is not automatically added to the chat history of the current page.
Sent message appears only after reloading the page.

Just a quick glance, the response from the server when sending the message is adequate, maybe something went wrong in the event handlers in the js code.
 
Alexey Navoykov:

It was about the specific context of the discussion(variable initialisation), not in general.

As far as I remember it was about initializing a STATIC variable

Forum on trading, automated trading systems and strategy testing

Bugs, bugs, questions

Alexey Navoykov, 2017.10.17 17:16

Bug with initialization of static variables. It was not present in old builds.

class A
{
 public:
  static int f()
  { 
    static int a=1;
    Print(a);       // Получаем a=0 !!
    return a;  
  }   
};


int a= A::f();


void OnStart()
  {
   
  }

Who doesn't mind, send it to service-desk. I have no desire to communicate with them there anymore.


Or is this not your message?

Sort out the variable initialisation sequence already. In this example a global variable is initialized first

int a= A::f();

which calls a function that hasn't yet initialized a variable of the same name.

static int a=1;

And what can we talk about if you don't pay attention to the compiler's warnings...


 
Alexey Navoykov:

But I set the wrong array dimension there, maybe it affected it somehow (although it shouldn't affect it at all).

Try this with dimension = 3

Thank you for the message.
Indeed, it is the compiler's optimizer error.
The fix will be included in the next build.
 
Alexey Viktorov:

As far as I remember it was about initialising a STATIC variable

Or is that not your message?

I don't understand what you want. I said it was about "variable initialization". And in this context static and global variables behave identically. I even gave you a link to documentation:variable initialization

Note that static and global variables are everywhere there joined together.

And we don't criticize the initialization sequence because it is not related to the problem at hand. Let me remind you that the problem is that the compiler will NOT generate an error where it should. If a static variable is still uninitialized, you cannot access it.

And what can we talk about if you ignore the compiler's warnings...

Well, change the global variable's name, if it bothers you that much. It won't affect the result in any way.

Reason: