Errors, bugs, questions - page 2577

 
Igor Makanu:

I know that option.

Then division by zero.

 
fxsaber:

Then division by zero.

Ng...there was hope, but as always only hardcore ))))

 
Vict:

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

Agreed

Forum on trading, automated trading systems & strategy testing

WinAPI -> MQL5 x64

A100, 2018.05.29 14:11

All the rule has been said. If you want it to work always - you need to use ushort, and if you want to understand why string suddenly stops working (what worked yesterday) - you can use string

 
Vict:

here you are going out of bounds


You gave me the idea to check the length of the initialized string in StringInit(out, 165, 32);

The StringInit help says that the second parameter is the string length, the string length in my understanding is the number of characters of the string.
In my case, the incoming string has 164 characters, so I put 165 in the second parameter, and tried more than *2, 330.

Usingmemcpy_s with a pointer,

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

The result was the same, the end of the string always drifts by the number of characters, and there are leaks when parsing.
Without a pointer the string is also floating.

But you gave me an idea and in StringInit(out, 1400, 32) I set the pointer size in bytes, not the string length.
And voila miracle, the string became without extra output characters, no problems parsing, and not a single leak.
I want to note that it is with the pointer in sizeof, without the pointer the string floats.

This begs a question to developers, is help on StringInit correct ???
Is not it mixed up, in the help or in the StringInit function itself, the length of the string with the size of the string?
In general now if you use memcpy it works correctly if you pass the pointer size and StringInit is initialized with this size, not the length!

 
A100:

Agreed

Yes, I agree too. But it's not always convenient to use string as an array, especially when this string needs to be passed through several functions.
Pointers are more convenient in this case, you don't need to re-fill this array in each transit function, and additionally enter one more parameter for the array, its size.

 
A100:

So you've simply allocated a buffer that is deliberately oversized (pushed back the boundary) and the error simply doesn't show up - because it was at the boundary

I thought so too, the string length is 164, set 165, set 330.
330 is clearly not a boundary of string length, if you count strings in characters.
It turns out that it's not the length, but the size of bytes.
But the help suggests the opposite, that it should be the length inStringInit.

In general, as I understood the mql's string type is a pointer and you should handle this type like a pointer.

 
Also decided to check the received string in the dll, for the presence of terminal null.
The zero is present
for (int index = 0; index <= wcslen(data); ++index)
   wcout << static_cast<int>(data[index]) << " ";
123 34 101 34 58 34 97 103 103 84 114 97 100 101 34 44 34 69 34 58 49 53 54 57 54 50 52 50 50 57 50 51 54 44 34 115 34 58 34 66 84 
67 85 83 68 84 34 44 34 97 34 58 49 54 54 50 49 57 49 55 53 44 34 112 34 58 34 56 50 48 50 46 49 48 48 48 48 48 48 48 34 44 34 113 
34 58 34 48 46 48 51 51 50 51 48 48 48 34 44 34 102 34 58 49 56 51 57 54 49 54 55 50 44 34 108 34 58 49 56 51 57 54 49 54 55 50 44 
34 84 34 58 49 53 54 57 54 50 52 50 50 57 50 51 49 44 34 109 34 58 116 114 117 101 44 34 77 34 58 116 114 117 101 125 0
 
Roman:

If you insist, have it your way.

 
Roman:

I thought so too. The string's length was 164, I set it to 165 and then to 330.
330 is obviously not a string-length boundary if you count strings in characters.
It turns out that it's not the length, but the size of bytes.
But the help suggests the opposite, that it should be the length in StringInit.

In general, as I understood the mql's string type is a pointer and you should handle this type like a pointer.

The mql type string is a structure that contains a pointer, the length of the string and possibly something else.

When you pass a string to the DLL, only the pointer is passed. The length of the string must be passed in manually.

And always make sure not to oversize the string.

All changes in string size are on the MQL side only.

On the DLL side, you should carefully read the help for the functions that you use. This is the only problem you have.

sizeof( wchar_t* ) returns the pointer size. That's not what you need at all.

 
Koldun Zloy:

The mql type string is a structure that contains a pointer, the length of the string and possibly something else.

When you pass a string to the DLL, only the pointer is passed. The length of the string must be passed in manually.

And always make sure not to oversize the string.

All changes in string size are on the MQL side only.

On the DLL side, you should carefully read the help for the functions that you use. This is the only problem you have.

sizeof( wchar_t* ) returns the pointer size. This is not what you need at all.

Right, I allocate a buffer for the string out, and initialize it with spaces.
Then I pass this string (pointer) to dll.

string out;
StringInit(out, 1400, 32);

Func(out);

In dll, wchar_t* data is copied to out, i.e. also a pointer. Logically there should be no problems.
As I understand it according to the help, the StringInit function should set the string length.
But I still have some problems withthe StringInit function itself; I specified the string length and got weird when I pointed to the pointer size.
I don't understand what manual string length transfer you mean.

And if you use sizeof(wchar_t) without a pointer, the string starts floating around with extra characters, which causes problems with parsing and leakage.
To pass strings into dll I used Renat's example, from his article on how to write a dll.
But for some reason, if I pass it without sizeof(wchar_t) pointer, the string floats, while with sizeof(wchar_t*) pointer there is no problem.
It seems logical to me, I'm copying a string as a pointer, the size should be passed to the pointer, not type.

Reason: