473,378 Members | 1,412 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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..?
Mar 13 '07 #1
3 35267
Banfa
9,065 Expert Mod 8TB
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.
Mar 13 '07 #2
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?
Mar 14 '07 #3
Banfa
9,065 Expert Mod 8TB
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.

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.
Mar 14 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Spare Change | last post by:
I am told that I can have a dynamic or static string array. So if I declare string dynamic; How do I add elements to dynamic and resize it ?
4
by: Robert | last post by:
Hi, How can i resize an array of strings to add more? This is an example i just downloaded from a website: char *cpArray; int i; for (i = 0; i < 10; i++) cpArray = (char *)malloc(20 *...
3
by: TobyD | last post by:
I am a beginner programmer in VB and need help. If I have a string with a return, how do I exchange the return with a return and tab? Currently I am entering a string with EnterKeyBehavior...
24
by: Sillaba atona | last post by:
I use this code to read dynamic string: char *s1; ....... puts("Inserire una stringa: "); while((*s1++=getchar())!='\n'); *s1='\0'; The compilation (ANSI C) is OK but I receive an error...
7
by: DarthBob88 | last post by:
I have to go through a file and replace any occurrences of a given string with the desired string, like replacing "bug" with "feature". This is made more complicated by the fact that I have to do...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.