Sign in to follow this  
CTEagle

strncpy_s vs. strncpy

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this