We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,263 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

warning C4996: 'strncpy' was declared deprecated

Hello,

I am not sure what the error means. I think it has to do with how I called the strncpy function. My error is:

.cpp(70) : warning C4996: 'strncpy' was declared deprecated

My code is the following:

// set the tool name
void Tools::setToolName( string toolNameString)
{
   // copy at most 20 characters for the tool name
    const char *toolNameValue = toolNameString.data();
    int length = int(toolNameString.size());
    length = ( length < 20 ? length : 19 );
    strncpy( toolName, toolNameValue, length);
    toolName[ length ] = '\0'; // append null character to lastName
}
5
Contributors
5
Replies
12 Hours
Discussion Span
4 Years Ago
Last Updated
6
Views
Question
Answered
QuantNeeds
Junior Poster in Training
96 posts since May 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Don't worry, it's not an error: it's a VC++ warning. Ignore it (in this case). See notes in MSDN help page on this function (click strncpy then press F1).

ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,239
Solved Threads: 349
Skill Endorsements: 6

It means that strncpy is old and/or something better is around to use instead of it.

CoolGamer48
Posting Pro in Training
401 posts since Jan 2008
Reputation Points: 77
Solved Threads: 40
Skill Endorsements: 0

MS (and probably other compiler vendors) are revising the string function to ensure greater safety. With strcpy, strcat and their variations, it's very easy to try to stuff more into a string array than it can hold. Therein lies the opening that many malware authors have exploited.

You can update your code to use the secure versions (they have an "_s" suffix and pass a parameter that is the size of the destination array), or continue with the old tried and true versions, adding the following to your code to suppress the warnings.
~~~~~~~~
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_NONSTDC_NO_DEPRECATE

Use these #define's to halt the many warnings given by
Visual C++ 2005/2008 for use of "unsafe" string functions.
Place them before your #include statements.

see the link below for a full list of the functions that have
secure variations available

http://msdn2.microsoft.com/en-us/library/ms235384(VS.80).aspx

vmanes
Postaholic
2,015 posts since Aug 2007
Reputation Points: 1,283
Solved Threads: 242
Skill Endorsements: 6

Of course the real answer would be to use another std::string which is totally safe, as opposed to some non-portable API which is only safe so long as the programmer knows what they're doing.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,875
Solved Threads: 953
Skill Endorsements: 27
Question Answered as of 4 Years Ago by Salem, vmanes, CoolGamer48 and 1 other

thank you - this worked

QuantNeeds
Junior Poster in Training
96 posts since May 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0765 seconds using 2.8MB