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 something 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:
Yes, you should delete the space you allocated after you’re done using it. That would look something like:
OTOH, you probably shouldn’t do any of this, and instead use something like:
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.