MS went on a "these standard functions aren't safe!" rampage a couple years ago and "deprecated" several standard functions, replacing them with _s ("safe") alternatives.
strcpy is potentially unsafe because it can lead to buffer overflow if you try to copy a string to a buffer that is not large enough to contain it:
1 2 3 4 5
|
char foo[10]; // a buffer able to hold 9 chars (plus the null)
char bar[] = "A string longer than 9 chars";
strcpy( foo, bar ); // compiles ok, but VERY BAD because you have a buffer overflow
// and are corrupting memory.
|
strcpy_s is "safer" because you have to explicitly specify the size of the target buffer, so the function will not overflow:
|
strcpy_s( foo, 10, bar ); // strcpy_s will not write more than 10 characters
|
The downside to this is that strcpy_s is non-standard and MS specific... and therefore if you write code to use it, your code will not be portable.
And truncating string data, while better than overflowing a buffer, is still bad and you should not write code in which this situation would ever come up (ie: you shouldn't be calling strcpy
OR strcpy_s unless you're
already sure the buffer is large enough to hold the string).
So personally... I define _CRT_SECURE_NO_WARNINGS in all my projects to shut these warnings up. Though I rarely use these functions anyway as C++'s std::string is not only safer than both of them, but also is much easier to use.