strncpy_s vs. strncpy

Started by
3 comments, last by CTEagle 18 years, 1 month ago
I have been learning about using strings in C++. The book I am reading had an example for copying strings. It suggested using strncpy. When I ran my code, I receivend an error stating that "'strncpy' was declared deprecated". The program ran but I ended up with a bunch of odd looking characters in my console window. The error message also stated that I should use strncpy_s. I used strncpy_s and my program worked correctly. In addition, the odd looking characters are no longer in my console window. So now I have a couple of questions. 1. What is the difference between strncpy and strncpy_n? 2. Why did using strncpy produce those odd looking characters? 3. Is there any issues I should be aware of if I use strncpy_n instead of strncpy? Thanks.
Advertisement
Quote:Original post by CTEagle
I have been learning about using strings in C++. The book I am reading had an example for copying strings. It suggested using strncpy.


If you have a book that says "C++" someplace on the front and contains the function "strcpy", throw it right out the window. Seriously, it's either really old or really horribly written. That's the old C way of doing things.

Do a Google search for "C++ strings"/"std::string"(and buy yourself a new book). It should eliminate the problems you seem to be having.

Edit: this one seems OK.

Edit 2: and this PDF
Quote:Original post by CTEagle
The book I am reading had an example for copying strings. It suggested using strncpy.

Throw your book away. Seriously. In C++ you should be using std::string.

If you were using C (and in rare cases in C++) then you might have reason to use a strncpy function.

1. strncpy is a standard function. Microsoft decided that strncpy wasn't safe enough and, wrongly in my opinion, deprecated it in their latest compiler (regardless of safety it is the standards organisation's job to deprecate and replace functions). strncpy_s is currrently non-standard and takes an extra parameter defining the size of the destination array.

2. It's impossible to say why you saw different results but there must have been some sort of error in your original program.

3. Not really.

Σnigma
Quote:Original post by CTEagle
1. What is the difference between strncpy and strncpy_n?
2. Why did using strncpy produce those odd looking characters?
3. Is there any issues I should be aware of if I use strncpy_n instead of strncpy?


1. strncpy, if used incorrectly, is more vulnerable to buffer overflow errors (i.e. writing more data than you have allocated memory for, thus corrupting memory possibly used by something else) than strncpy_s. I seem to recall it had something to do with strncpy not null-terminating the string output if it was too long to fit in the buffer.
2. You likely have a buffer overflow problem somewhere in your code.
3. strncpy_s is not standard; it only works on Microsoft compilers.

As others have said, it's a far better idea to just switch to std::string (part of the Standard C++ Library) unless you absolutely have to use strncpy. Also, to turn off the deprecated warning, simply #define _CRT_SECURE_NO_DEPRECATE before you include your standard headers.
Thanks to everyone that took the time to respond.

Scet - Both of those links were great. I have some experience in VB and it seems std::string is a little closer to the way I have been doing things than strncpy.

This topic is closed to new replies.

Advertisement