Memory allocation

What is the right way to use (and Why?)

wchar_t *ploc = NULL;
ploc=new wchar_t[4096];

1.       ploc = OLE2W( bsLocation);
2. wchar_t* ploc =  OLE2W( bsLocation);
or use wcsncpy?

or is there any better way....
Who is Participating?
rajeev_devinConnect With a Mentor Commented:
This is ok
wchar_t* ploc =  OLE2W( bsLocation);
Deepu AbrahamConnect With a Mentor R & D Engineering ManagerCommented:
Hi pb_india,

You can see this link:

My answer would be, you can use all the points you have mentioned but that may depend on your program.

only caution in using OLE2W()/A2W() etc.. is you need to put this inside a function if it is getting called inside a loop.

wchar_t *ploc = NULL;

void foo()
     ploc=new wchar_t[4096];
     ploc = OLE2W( bsLocation);

Please refer to this site:USES_CONVERSION - a cautionary tale

Also look for CA2WEX Class in MSDN.

Best Regards,
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.