I'm looking for a way to automatically deallocate an array of wchar_t
s – kind of like an autopointer (I'm not really aquainted with std::auto_ptr, but I think it cannot be used for arrays).
The code I have right now is this:
/* volume is of type wstring,
* hr is of type HRESULT,
* VSS_PWSZ equals wchar_t*
*/
VSS_PWSZ pwszVolume = new wchar_t[volume.size() + 1];
std::copy(volume.begin(), volume.end(), &pwszVolume);
pwszVolume[volume.size()] = 0;
hr = pDiffMgmt->QueryDiffAreasOnVolume(pwszVolume, &pEnumMgmt);
delete[] pwszVolume;
pwszVolume = NULL;
I don't really get why this stupid function cannot take a const wchar_t*
, otherwise I could just pass volume.c_str()
.
So far so good, I think my code solves this problem, but now the memory management is getting more complicated: I would have to duplicate the delete[]
code to account for exceptions which might be thrown (and which I do not want to catch at this point.)
Is there a way I can get pwszVolume
to be deallocated automatically when the current scope is left?