Memory allocation

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

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

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

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

You can see this link:
http://msdn2.microsoft.com/en-us/library/805c56f8.aspx

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()
{
    USES_CONVERSION;
     ploc=new wchar_t[4096];
     ploc = OLE2W( bsLocation);
}

Please refer to this site:USES_CONVERSION - a cautionary tale

http://untidy.net/blog/category/vc/

Also look for CA2WEX Class in MSDN.

Best Regards,
DeepuAbrahamK
0
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.