Features of the mql5 language, subtleties and tricks - page 161

 
printf("%s , %f , %i",(string)NULL,(double)NULL,(int)NULL);

2020.01.27 01:15:57.859 tst (EURUSD,H1) (null) , 0.000000 , 0

if I remove type conversion, the 'NULL' - expression of 'void' type is illegal

interesting, it turns out that nput string can be initialized void

 
fxsaber:

NULL is initialisation. The example purposely shows a non input string.

NULL / NIL (in other terms) cannot be an initialization of a constant string entity.

At least because the mql string is not a "pointer" like in C. It has no special state "nothing at all".

PS (oh, I did not expect that from myself)

 
Maxim Kuznetsov:

She has no special state of "nothing at all"

Forum on trading, automated trading systems and trading strategies testing

Peculiarities of mql5, tips and tricks

fxsaber, 2020.01.26 15:54

void OnStart()
{
  uchar Bytes[];

  Print(StringToCharArray(NULL, Bytes)); // 0
  Print(StringToCharArray("", Bytes));   // 1
}

I use this condition every day.

 
Maxim Kuznetsov:

at least because an mql string is not a "pointer" like in C. It has no special state "nothing at all".

PS (oh, I didn't expect that from myself).

alas no, a string in MQL is an entity with allocated memory.@Ilyas explained why StringBufferLen() may not show the string length, but shows the memory used to store the string.


fxsaber:

I use this condition every day.

It's no indicator at all, in MQL4 I unset pointers to any object using StringConcatenate(), i.e. the built-in functions work is not implemented by MQL rules )))

input string s = NULL;
void OnStart()
{
   uchar Bytes[];
  Print(StringToCharArray(NULL, Bytes));  // 0
  Print(StringToCharArray("", Bytes));    // 1
  Print(StringToCharArray(s, Bytes));     // 1
}
 
Igor Makanu:

It's no indicator at all, I unset pointers to any object in MQL4 using StringConcatenate(), i.e. the built-in functions are not implemented by MQL rules )))

Doesn't make any difference what's inside.

Forum on trading, automated trading systems and strategy testing

Peculiarities of mql5, tips and tricks

Igor Makanu, 2020.01.26 22:27

input string s = NULL;
void OnStart()
{
   uchar Bytes[];

  Print(StringToCharArray(s, Bytes));     // 1
}
Because it's not NULL. That's what the original message was about.
 
fxsaber:

It makes no difference how it is inside.

supplemented my post with your example


I also did a couple more experiments with input string s = NULL;

as far as I understood, while we're not trying to use input variable's value - it will be NULL, but the moment we try to assign value to another variable or simply unset it, NULL will disappear, i.e. memory will be allocated to this input variable

 
Igor Makanu:

as far as I understand, while we're not trying to use the value of an input variable - this value is NULL, but at that moment, if we try to assign the value of this input variable to another variable or simply unset this input variable, then this NULL will disappear, i.e. memory will be allocated to this input variable

The mechanism of input variable operation has been described in detail above.

 
fxsaber:

It doesn't make any difference what's inside.

Because it's not NULL. That's what the original message was about.

there is (still) specificity of entity dereferencing (there are no pointers in mql, it's not C, let there be entities)

input is a const declared at startup. you cannot physically give it the value "NULL".

const string nothing=NULL; /// <--- ЭТО ЧТО ?

 
Nikolai Semko:

Yes, this behaviour is probably just "foolproof".
Apparently, because input variable must be initialized, the= NULL is artificially equated to ="", when the variable already occupies at least one byte in memory.

In MQL5 the entries:

Are absolutely identical. No memory is allocated for the variable. Anyway, I haven't found any differences.
In java, for instance, such records are slightly different, despite the fact that no memory is allocated for the variable in both cases, too. The first variant (String str = null;) will be considered initialized, although no memory is allocated for the variable and the variable can be printed as empty. In the second case (String str;) when you try to print the variable, an error of uninitialized variable will be generated.
I.e. MQL5 is more tolerant in this regard.
Which is better, I don't even know.

How is memory not allocated to a variable?
The memory is allocated and the variable contains arbitrary rubbish.
And the fact that string does not output any values, it is logical that the buffer is empty.

char   ch; 
short  sh;
int    in;
double db;       
string st;

Print(sizeof(ch));
Print(sizeof(sh));
Print(sizeof(in));
Print(sizeof(db));
Print(sizeof(st));
Print("---------------------");
Print(ch);
Print(sh);
Print(in);
Print(db);
Print(st);
 
Roman:

How is memory not allocated to a variable?
The memory is allocated, but the variable does not contain arbitrary rubbish.
And the fact that string does not output any values, it is logical that the buffer is empty.

char ch; 
short sh;       
string st;

Print(sizeof(ch));
Print(sizeof(sh));
Print(sizeof(st));

   string str1;
   string str2="Однажды, в студёную зимнюю пору я из лесу вышел; был сильный мороз.";
   Print(sizeof(str1));  // 12
   Print(sizeof(str2));  // 12
   uchar ch1[],ch2[];
   Print(StringToCharArray(str1, ch1));  // 0
   Print(StringToCharArray(str2, ch2));  // 68
Reason: