Re: strcpy_s vs strcpy
On Jul 3, 7:53 am, Thomas Richter <t...@math.tu-berlin.de> wrote:
I wonder why there is a need for an additional interface if there are
already functions that do that for you? Or to put this in different
words, what's wrong with strncpy() that makes strcpy_s necessary?
For one, strncpy() has no parameter specifying the size of the
destination buffer - and therefore strncpy() has no way to verify
that the destination buffer will not be overrun by the copied
characters. For another, strcpy_s enforces a set of "runtime
constraints": whereas calling strncpy() with a NULL pointer or with a
negative number of characters to copy will result in undefined
behavior, calling strncpy_s() with those arguments will cause a
runtime constraint violation - but will not result in any undefined
behavior. Lastly, (and more subtly) strcpy_s guarantees that the
string in the destination buffer will be nul-terminated. Because, if
the result is of the string copy operation is not a nul-terminated
string, then even calling strlen() on it can result in undefined
behavior.
Greg
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]