I am migrating C++ code to VS2005, for this i have to replace the some Deprecated CRT Functions like “strcpy” by strcpy_s
I have written template to replace the function call. For strcpy_s I need to pass destination buffer size. This is possible for statically allocated string but not working for dynamically memory allocated strings as can’t determine size of the buffer at compile time.
The code which I had written is as below,
So can we write a single template which will work for both of these cases..?
I have written template to replace the function call. For strcpy_s I need to pass destination buffer size. This is possible for statically allocated string but not working for dynamically memory allocated strings as can’t determine size of the buffer at compile time.
The code which I had written is as below,
Code:
template <class T> errno_t SafeStrCopy (T* szDest, const T* szSrc) { return strcpy_s(szDest,sizeof(szDest),szSrc); } #define strcpy SafeStrCopy Void main () { char sz_Dest[20] = "Hello World"; char sz_Source[20]= ""; char* dynStr = new char[20]; strcpy(sz_StrTo,sz_StrFrom); //This works fine as it’s static strcpy(dynStr,sz_StrFrom); //Doesn’t work as it’s dynamic }
Comment