1,105,585 Community Members

warning C4996: 'strncpy' was declared deprecated

Member Avatar
QuantNeeds
Junior Poster in Training
96 posts since May 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
}
Member Avatar
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,090 [?]
Q&As Helped to Solve: 347 [?]
Skill Endorsements: 8 [?]
 
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).

Member Avatar
CoolGamer48
Posting Pro in Training
401 posts since Jan 2008
Reputation Points: 65 [?]
Q&As Helped to Solve: 40 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
vmanes
Postaholic
2,141 posts since Aug 2007
Reputation Points: 1,165 [?]
Q&As Helped to Solve: 282 [?]
Skill Endorsements: 10 [?]
 
1
 

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

Member Avatar
Salem
Posting Sage
7,177 posts since Dec 2005
Reputation Points: 5,138 [?]
Q&As Helped to Solve: 970 [?]
Skill Endorsements: 41 [?]
Team Colleague
 
0
 

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.

Question Answered as of 5 Years Ago by Salem, vmanes, CoolGamer48 and 1 other
Member Avatar
QuantNeeds
Junior Poster in Training
96 posts since May 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

thank you - this worked

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article