Errors, bugs, questions - page 2578

 

Try

StringInit(out, 200, 32);
...

if ( memcpy_s(out, 200*sizeof(wchar_t), data, wcslen(data)*sizeof(wchar_t)+sizeof(L'\0')) ) {
   // что-то пошло не так
}

After this has worked, I would turn my attention to wcscpy.


 
Vict:

Try

After this has worked, I would turn my attention to wcscpy.


No, it didn't work that way, the string isn't passed, but there's no problem either.
This works without the zero, but the end of the string is floating around with extra characters.

StringInit(out, 200, 32);
if (memcpy_s(out, 200 * sizeof(wchar_t), data, wcslen(data) * sizeof(wchar_t)))     // + sizeof(L'\0'))) 
{
   MessageBoxW(NULL, L"Problem", L"Problem", MB_OK);
}
 
I initialise with a pointer size, and use a pointer in memcpy, so it's OK :))
StringInit(out, 1400, 32);

if (memcpy_s(out, 200 * sizeof(wchar_t*), data, wcslen(data) * sizeof(wchar_t*)))  
{
   MessageBoxW(NULL, L"Problem", L"Problem", MB_OK);
}
 
Roman:


No, it doesn't work that way, the string is not passed, but there is no problem either.
But it worked like this, but again the end of the string is floating around with extra characters.

Read the std::wcslen docs carefully.

Returns the length of a wide string, that is the number of non-null wide characters that precede the terminating null wide character.

If don't undestand, google help mi:

Returns the length of a wide string, that is, the number of non-null wide characters that precede the terminating null wide character. If we don't believe it at all, we write a check:

#include <iostream>
#include <cwchar>

int main()
{
        const wchar_t* text = L"12345";
        std::cout << std::wcslen(text);
        return 0;
}
Launch and see:

Rewrite:

if (memcpy_s(out, 200 * sizeof(wchar_t), data, (wcslen(data)+1) * sizeof(wchar_t))) 

Conclusion: First we read the docks, do not understand, go to the forum, read the forum, remember that someone who writes on the forum, probably the same guru as you are, respectively, after the forum again open docks, write a test, ohrenzied by the knowledge of a guru on the forum, consider themselves smart as a duck, write code, ohrenzied by bugs, again a cycle docks-forum, learn about ub, find where the mistake, ohrenzied at his stupidity.

This is roughly how it works.

PS. This is C, this is hardcore, here you should not smoke forums, only serious publications and docs, only your own brains.

 
Roman:


No, it did not work, the string is not passed, but there is no problem either.
But it worked this way without zero, but it floats the end of the string with extra characters.

If it didn't work, then an empty string was fed to the input. My option is correct and working, yours is not.

#include <cstring>
#include <iostream>
using namespace std;

int main() {
        wchar_t out[200];
        wchar_t data[] = L"Hello world";
        memcpy(out, data, wcslen(data)*sizeof(wchar_t)+sizeof(L'\0'));
        wcout << "string = " << out << endl;
}
// string = Hello world

You bluntly overwrite the buffer with a large margin, naturally the desired line is captured as well.

 
Vladimir Simakov:

PS. This is C, this is hardcore, no forums to smoke here, only serious publications and docs, only your own brains.

This hardcore(wcslen(data)+1) I've already tried, and now I've tried your version, the string is not copied at all.

 
Roman:

This hardcore(wcslen(data)+1) I've already tried, and now I've tried your option, the string doesn't copy at all.

And what does the second parameter in memspy_s function mean?

 
Vladimir Simakov:

What does the second parameter in the memspy_s function mean?

Let me try to guess

- string length out., only +1 character, which /000 forgot.

 
Vict:

If it didn't work, then an empty string was fed to the input. My option is correct and working, yours is not.

You are stupidly overwriting the buffer with a large margin, of course, that the right string is captured as well.

How is an empty string? When the string comes from a socket, in other variants though it's crooked, but it's not empty.
I'm not saying your variant is wrong, but in your variant you use an array of string.
In my case it's a pointer.

const wchar_t* data = getData();

The socket function returns a pointer to const wchar_t*.
Because I blame it on bug in mql, because I've tried so many different C functions, hardcore +1 or +2.
It just won't work.

 
Vladimir Simakov:
Let me try to guess

- string length out., only +1 character, which /000 forgot.

If you mean about this

StringInit(out, 201, 32);

if (memcpy_s(out, 201 * sizeof(wchar_t), data, (wcslen(data) + 1) * sizeof(wchar_t)))

Then the string is copied very rarely, and with big time lags.
This variant has already been tried at the very beginning, go back a couple of pages, I posted screenshots there.

Here's the same behavior as in the second screenshot, the modders why the names of files on the krakozrabryy replaced.

You see, even this functionwcscpy(out, data); which automatically counts string length, causes the same behavior, time skipping the entire string.