std::wcsrtombs made my pointer nullptr. Is it ok?

Artem Basov 21 Reputation points
2022-03-14T16:59:03.377+00:00

I use VS2019.

I have my string class, and I want my wchar_t string will return new char string

String_base<char> to_utf8() const  
{  
	String_base<char> output;  
  
	std::mbstate_t state = std::mbstate_t();  
	std::size_t len = 1 + std::wcsrtombs(nullptr, (const wchar_t**)&m_data, 0, &state);  
	std::vector<char> mbstr(len);  
	std::wcsrtombs(&mbstr[0], (const wchar_t**)&m_data, mbstr.size(), &state);  // <<<<<<<<< AFTER HERE  
  
	output.append(&mbstr[0]);  
	return output;  
}  

and after std::wcsrtombs m_data is nullptr

I added this before all actions

const wchar_t* old = m_data;  

and old points to string(everything good), so std::wcsrtombs for some reason made CONST pointer nullptr.
182929-screenshot-2022-03-14-195408.png

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,736 questions
{count} votes

Accepted answer
  1. Igor Tandetnik 1,106 Reputation points
    2022-03-15T02:25:41.3+00:00

    From the documentation:

    The conversion stops if:

    • The null character was converted and stored. src is set to a null pointer...

    So yes, setting m_data to null is the expected and documented side effect of the function you use.

    wcsrtombs did not make a const pointer null. In the type const wchar_t**, it's the wchar_t - the pointed-to data - that is const. Not the pointer that points to it (that would have been wchar_t* const *)

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.