Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

clone a wchar_t* in c++

Tags:

c++

visual-c++

I want to clone a path that is held in a var named szPath to a new wchar_t.

alt text

szPath is of the type wchar_t *. so i tried doing something like:

szPathNew = *szPath;

but this is referring to the same place in memory. what should i do? i want to deep clone it.

like image 389
Tom Avatar asked Dec 03 '22 03:12

Tom


1 Answers

Do this,

wchar_t clone[260];
wcscpy(clone,szPath);

Or, if you want to allocate memory yourself,

wchar_t *clone = new wchar_t[wcslen(szPath)+1];
wcscpy(clone,szPath);
//use it
delete []clone;

Check out : strcpy, wcscpy, _mbscpy at MSDN

However, if your implementation doesn't necessarily require raw pointers/array, then you should prefer this,

#include<string>

//MOST SAFE!
std:wstring clone(szPath);
like image 136
Nawaz Avatar answered Dec 20 '22 14:12

Nawaz