开发者

C++ Creating a simple array of LPWSTR's

开发者 https://www.devze.com 2023-03-28 01:20 出处:网络
As a C++ noob, I can\'t seem to get this right even though I know it should be simple :{ - I need to create an array of LPWSTR and then populate it with unique strings. The idea was to do somethi开发者

As a C++ noob, I can't seem to get this right even though I know it should be simple :{ - I need to create an array of LPWSTR and then populate it with unique strings. The idea was to do somethi开发者_如何学编程ng simple like this:

LPWSTR *wszArray = new LPWSTR[5];

for(int x = 0; x < 5; x++)
{
    swprintf(wszArray[x], "somestring_%d", x);
}

I know that I haven't allocated memory for the LPWSTR, but after trying a few things I am not having much luck. Also I'm not sure if the array should be free'd later once i'm done with the strings.

Any advice would be great.


What you have right now is a single pointer to pointers to wide char. You're then initializing that with the address of an array of 5 dynamically allocated pointers to wide char's. That's fine as far as it goes, but does not allocate any space for the strings themselves, only for pointers to 5 strings. You then need to allocate space for the strings themselves:

for (int i=0; i<5; i++) {
    array[i] = new char[13];
    swprintf(array[i], L"somestring_%d", i);
}

Yes, you should delete the space you allocated after you're done using it. That would look something like:

for (int i=0; i<5; i++)
    delete [] array[i];
delete [] array;

OTOH, you probably shouldn't do any of this, and instead use something like:

std::vector<std::wstring> array;

for (int i=0; i<5; i++) {
     std::wostringstream temp;
     temp << L"somestring_" << i;
     array.push_back(temp.str());
}

In this case, you can retrieve a "LPWSTR" using array[i].c_str(). You don't have to allocate or free any of the memory explicitly at all though.


actually LPWSTR is already a pointer you should rather do

LPTSTR pBuffer; // TCHAR* 
pBuffer = new TCHAR[128]; // Allocates 128 or 256 BYTES, depending on compilation.

and then the for loop will be fine. for unicode-long string you need to prefix the string with L like in L"your string"

and later you need to free the memory allocated using delete[]

like :

delete[] pBuffer;

for further documentation you may like to read:

http://www.codeproject.com/Tips/76252/What-are-TCHAR-WCHAR-LPSTR-LPWSTR-LPCTSTR-etc


LPWSTR is a unicode long string pointer in MS Windows, try using strings with the L"" form such as:

swprintf(wszArray[x], L"somestring_%d", x);

Unicode macros in Windows are available by using #define UNICODE, in MS VS there is a setting to use unicode strings, when set, it will define UNICODE for you.

LPWSTR is the same as wchar_t* and LPSTR is char*.

When allocate memory in c++, it should always be free'd, so the answer is Yes.


Learning and doing some research-and-development is different aspect. But you shoulnt use these native methods for string processing. Instead use classes like std::string, stringstream, CString etc.

  • std::string is used by many (NOT by me!), it doesn't support << operator, nor formatting functions, but supports other functions.
  • stringstream is rich, doesn't support formatting functions, but << operators, which are resolved at compile time and are type-safe.
  • CString supports formatting functions, and other common string functionality. It doesn't support << operator. It handles ANSI/Unicode issues quite well. If you arent using MFC, you still can use it by #include<atlstr.h> in non-MFC project.


I am actually also new in C++ (only finish Deitel c++ book, nothing more), I like CString only :-( if I agree to allow the project to accept MFC library in the project settings, I include the whole library in the header file if I feel like to, then I can do something like

CString str="";
CArray<CString,CString> arr;
for(int i=0;i<n;i++)
{ 
   str.Format("something_%d",i);
   arr.Add(str);
} 

If you really really love LPWSTR, the above also works with it. CString is a class designed to handle ANSi and Unixcode... :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消