Home > Back-end >  C would be and char conversion problem
C would be and char conversion problem

Time:11-29

(const char * _stdcall wchar2char would * wchar)
{
Int len=WideCharToMultiByte (CP_ACP, 0, wchar, wcslen (wchar), NULL, 0, NULL, NULL);
Char * m_char=new char [len + 1];
WideCharToMultiByte (CP_ACP, 0, wchar, wcslen (wchar), m_char, len, NULL, NULL);
M_char [len]='\ 0';
Return m_char;
}

Online example basic it is like that, I want to ask which used to release new don't delete words no problem? This code is only this or say not perfect?

CodePudding user response:

Using the new, should be released when no longer need to use the delete unused space,

CodePudding user response:

reference 1st floor Tryagain2006 response:
using the new, should be released when no longer need to use the delete unused space,

I posted this function is equivalent to every call is the new one not released

CodePudding user response:

New can be set within the function, can also in the external release, the function USES the request requires your after use, to release,
Return values for char * this method can ensure that you use the return pointer when there is no problem,
If you think this is unreasonable, we could do a little modification
Int _stdcall wchar2char (const would * wchar, char * m_char)
{
Int len=WideCharToMultiByte (CP_ACP, 0, wchar, wcslen (wchar), NULL, 0, NULL, NULL);
//char * m_char=new char [len + 1];
WideCharToMultiByte (CP_ACP, 0, wchar, wcslen (wchar), m_char, len, NULL, NULL);
M_char [len]='\ 0';
Return strlen (m_char);
}
Himself to add exception handling is ok, the transformed Pointers externally to you, so who can assign who released

CodePudding user response:


I encapsulates two kinds, one kind is returned out of char * delete
A return string stringw memory problems

CodePudding user response:

reference 4 floor smwhotjay response:

I encapsulates two kinds, one kind is returned out of char * delete
A return string stringw afraid of the memory problem

So the DLL is not can't use extern "C" to modify

CodePudding user response:

reference 5 floor wuzhifei reply:
Quote: refer to 4th floor smwhotjay response:

I encapsulates two kinds, one kind is returned out of char * delete
A return string stringw afraid of the memory problem

So the DLL is not can't use extern "C" to modify it

I don't use a DLL, I said encapsulation is the function

CodePudding user response:

Would turn char,
Give a wchar outside come in, are you still a char,
Char you where? Rice also have bowl,
Or let come over rice bowl of people bring their own guy,
Or you get a bowl for good back,

You said foreign you bowl, rice enough dropper,
Results, in fact, people eat will give you throw the bowl, finally you find that you ran out of the bowl,

CodePudding user response:

I mostly use string and wstring replaced the new in function, finished with the delete,
 STD: : string_stdcall wchar2char (const would * wchar) 
{
Int len=WideCharToMultiByte (CP_ACP, 0, wchar, wcslen (wchar), NULL, 0, NULL, NULL);
Char * m_char=new char [len + 1];
WideCharToMultiByte (CP_ACP, 0, wchar, wcslen (wchar), m_char, len, NULL, NULL);
M_char [len]='\ 0';
STD: : string STR.
STR=m_char;
The delete [] m_char;
Return STD: : move (STR);
}

CodePudding user response:

refer to the eighth floor light bamboo hat reply:
I mostly use string and wstring replaced the new in function, finished with the delete,
 STD: : string_stdcall wchar2char (const would * wchar) 
{
Int len=WideCharToMultiByte (CP_ACP, 0, wchar, wcslen (wchar), NULL, 0, NULL, NULL);
Char * m_char=new char [len + 1];
WideCharToMultiByte (CP_ACP, 0, wchar, wcslen (wchar), m_char, len, NULL, NULL);
M_char [len]='\ 0';
STD: : string STR.
STR=m_char;
The delete [] m_char;
Return STD: : move (STR);
}

Write in the DLL return value is a string of words can't use extern "C"

CodePudding user response:

references 9 f wuzhifei response:
written in the DLL return value is a string of words can't use extern "C"

You can refer to a similar approach, such as design classes,
 class test1 
{
Public:
Test1 () {PTR=NULL; };
~ test1 () {if (PTR) delete [] PTR};

Void the resize (int length)
{
If (PTR) delete [] PTR.
PTR=new char (length),
}

Char * getData () {return PTR. }

Private:
Char * PTR.
}

Export function into
Bool_stdcall wchar2char (const would * wchar, void * out);
When call
Test1 t;
Wchar2char (L "Hello World", & amp; T);
  • Related