Connecting Tech Pros Worldwide Help | Site Map
Reply
 
LinkBack Thread Tools Search this Thread
  #1  
Old March 13th, 2007, 05:29 PM
Newbie
 
Join Date: Mar 2007
Posts: 3
Default Replacing 'strcpy' with 'strcpy_s' for dynamic string

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,


Expand|Select|Wrap|Line Numbers
  1. template <class T>
  2.  
  3. errno_t SafeStrCopy (T* szDest, const T* szSrc)
  4.  
  5. {     
  6.         return strcpy_s(szDest,sizeof(szDest),szSrc);       
  7.  
  8. }  
  9.  
  10. #define strcpy SafeStrCopy 
  11.  
  12. Void main ()
  13.  
  14. {
  15.  
  16.       char sz_Dest[20] = "Hello World";
  17.  
  18.        char sz_Source[20]= "";
  19.  
  20.        char* dynStr = new char[20];
  21.  
  22.       strcpy(sz_StrTo,sz_StrFrom); //This works fine as it’s static 
  23.  
  24.       strcpy(dynStr,sz_StrFrom); //Doesn’t work as it’s dynamic
  25. }
So can we write a single template which will work for both of these cases..?

Last edited by Ganon11; March 14th, 2007 at 03:05 PM. Reason: code tags added
Reply
  #2  
Old March 13th, 2007, 11:48 PM
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 5,479
Default

Quote:
Originally Posted by RavindraB
So can we write a single template which will work for both of these cases..?
No, in fact you will never be able to get a function like this working for a dynamically allocated string without passing the size of the string because all the code has to go on is the type of a the variable and for a dynamically allocated string that is a pointer.

strcpy is not deprecated, Microsoft say so but they do not write the C/C++ standards, on some *nix platforms strcpy is replaced by strlcpy.

The truth is that strcpy does have some security issues (through the risk of buffer over-run) and MS have created the language extension strcpy_s while other have created strlcpy, however neither has made it into the C/C++ standard (as far as I am aware) so far although there is a good case for doing so.
Reply
  #3  
Old March 14th, 2007, 10:22 AM
Newbie
 
Join Date: Mar 2007
Posts: 3
Default

Thanks for guidelines, we are following same approch..

My next questions are came up while I investigating on templates..
What is the difference between following two templates

template <class T>

T& strcpy1(T &tParam1, T &tParam2)

{

strcpy_s(tParam1, sizeof(tParam2), tParam2);

return tParam1;

}

template <class T>

T strcpy1(T *tParam1, T *tParam2)

{

strcpy_s(tParam1, sizeof(tParam2), tParam2);

return *tParam1;

}


Another question is also about template.
I found the template code as below,


template <int CCH>

inline HRESULT SafeStrCopy(char (&szBuffer)[CCH], const char *szStr)

{

C_ASSERT(CCH > 0);

return StringCchCopy(szBuffer, CCH, szStr);

}

Here its determine array size in CCH field, through “char &szBuffer)[CCH]”

But not cleared well to me.

How compiler calls this template when it invokes strcpy(sz_StrTo,sz_StrFrom);

Though it seems from template decalration that we need to pass arg as int ..?

What is difference between

template <int CCH> and template <class T> declarations?
Reply
  #4  
Old March 14th, 2007, 01:40 PM
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 5,479
Default

Quote:
Originally Posted by RavindraB
What is the difference between following two templates

template <class T>

T& strcpy1(T &tParam1, T &tParam2)

{

strcpy_s(tParam1, sizeof(tParam2), tParam2);

return tParam1;

}

template <class T>

T strcpy1(T *tParam1, T *tParam2)

{

strcpy_s(tParam1, sizeof(tParam2), tParam2);

return *tParam1;

}
The first declares a function that takes reference arguments the second a function that takes pointer arguments. The correct operation depends entirely on what the compiler chooses to use for class T.

Quote:
Originally Posted by RavindraB
template <int CCH>

inline HRESULT SafeStrCopy(char (&szBuffer)[CCH], const char *szStr)
{
C_ASSERT(CCH > 0);

return StringCchCopy(szBuffer, CCH, szStr);
}

Here its determine array size in CCH field, through “char &szBuffer)[CCH]”

But not cleared well to me.

How compiler calls this template when it invokes strcpy(sz_StrTo,sz_StrFrom);

Though it seems from template decalration that we need to pass arg as int ..?

What is difference between

template <int CCH> and template <class T> declarations?
Careful, template parameters are not arguments. Since sz_StrTo and the function takes a reference to an array of size CCH the compiler can work out the value of CCH required to call the function with sz_StrTo. Alterntitively it is possible that this template was always used with explicit values for it template parameters rather than trying to let the compiler work it out.

The difference between template <int CCH> and template <class T> is that the first declares a template with a generic value and the second declares a template with a generic type.
Reply
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 204,687 network members.
Post your question now . . .
It's fast and it's free

Popular Articles