Errors, bugs, questions - page 2576

 
Vict:

Your examples are funny, you removed everything, you left UB (string literal modification), and everyone has to telepathize. If you expect to get some smart advice, give minimal working code (on two sides), otherwise it's just rubbish.

The example shows the place which causes the problem, i.e. the problem is in copying of wchar_t* pointer to mql string.
The rest of the code is not relevant to the problem and is not useful, because it just checks if there is data, then read, etc.
Why write it in the example and contaminate the essence of the problem, when even with simplified code, many people will not understand what the problem is.
Understand,getData() is a network function that reads FrameOpcode, and returns the data received as a pointer to a string of type const wchar_t *.

Everyone knows that a simple functionwcscpy(out, data) copiesconst wchar_t * string intothe stringwchar_t *, and automatically counts the length of characters to terminal null const wchar_t *.
Here we encounter an error, mql string does not correctly accept acopied stringwchar_t *, so why? If function automatically detects terminal null.
In Renat's article, strings are copied via memcpy with byte size error. Perhaps the same approach is used in terminal code itself, to form mql-type string.
You see,
memcpy is not only not suitable for copying strings, but also with passed bytes size error, leads to jagged data.
There are other special C functions for string copying, such aswcscpy, wcsncpy, etc.
And Renat himself wrote in one of the threads that soon will be completely reworked working with strings, apparently the problem is known, but for some reason silence in response to the problem I have indicated.

Here is a comparison of byte size ofwchar_t* pointer and simplewchar_t type

Files:
1.PNG  83 kb
 
Roman:

Of course, it's easier to write a whole mess in response than a normal reproducible test by replacing getData() with something. What do you expect if UB on UB and the conclusions are wrong:

memcpy(cp,to,wcslen(to)*sizeof(wchar_t));  //в этой строке должен быть указатель sizeof(wchar_t *)

everything is right there. Something is wrong with your ideas about strings, hence the sprawl.

 
Vict:

Of course, it's easier to write a whole mess in response than a normal reproducible test by replacing getData() with something. What do you expect if UB on UB and the conclusions are wrong:

everything is right there. Something is wrong with your notions of strings, hence the errors.

There is no way to provide reproducible code, as you yourself understand that it is a dll using third party libraries.
As for why I thought there was an error in the example.
memcpy(cp,to,wcslen(to)*sizeof(wchar_t));//this string must containthe pointer sizeof(wchar_t *)

If we use the function without the pointer,

memcpy(out, data, wcslen(data) * sizeof(wchar_t));

it will cause the end of the string to be swamped with unnecessary characters. Look at the end of the string in the image.
And it is logical that if we copy a string wchar_t * as a pointer, we should pass the pointer's size and not the type's size.

And if we use a pointer,

memcpy(out, data, wcslen(data) * sizeof(wchar_t*));

the string is clear and without extra characters.
It wouldn't matter, but in both cases I have further problems with parsing, i.e. strings leaking and skipping.

And if I use this function, then nothing leaks, all parsing is good, only one extra character at the end of the line breaks, then it appears and then disappears.

wcsncpy(out, data, wcslen(data));

So I am going through a bunch of options, but using memcpy without a pointer to the sizeof, the result is shown in the screenshot.

I want to check received wchar_t * string for terminal null, whether it is there or not.
How can this be done?

Files:
 
Using a function without a pointer,

it leaves out without \0 at the end.

And if I use a pointer,

here you're out of bounds

And if I use this function, nothing leaks, everything is parsed fine, just one extra character at the end of the string, it appears and disappears.

out again without \0. See docs

wcsncpy, wcsncpy_s

...

If countis reached before the entire string srcwas copied, the resulting wide character array is not null-terminated.

...

HH: maybe don't mess with strings at all? save arrays in wchar_t and run them, and inside µl convert to string if needed https://www.mql5.com/ru/docs/convert/shortarraytostring

 
Vict:

ZS: maybe don't mess with strings at all? save arrays in wchar_t and run them, and inside the µl convert to string if needed

Oh, thank you )), for the tip that wcsncpy trims the zero.
Yes arrays are left for last, if pointers don't work, I'll use arrays.

 

The forum engine doesn't allow you to make a post from a single image. Requires you to enter text.

You have to put a space.

 
fxsaber:

The forum engine doesn't allow you to make a post from a single image. Requires you to enter text.

I have to put a space.

It seems to make sense: this is a forum and the main thing is the text. A picture attached to how much at a time. This is not a graveyard of images.

 

I finished the code, it works in MT4/5, but I faced a little surprise

How can I replaceTesterStop() in MQL4

?

 
Igor Makanu:

How can I replace TesterStop() in MQL4?

ExpertRemove.

 
fxsaber:

ExpertRemove.

I know this variant, but I've seen reports that ExpertRemove(0) cannot be used for Expert Advisors in the Market.

In general, I use TesterStop() in two cases:

- instead of INIT_PARAMETERS_INCORRECT to hide the optimizer's log

- if there are not enough funds to open an order, I do not open it, but I close the test to optimize it faster