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
}
Related Article: error C4996: 'strncpy'
is a solved C++ discussion thread by QuantNeeds that has 2 replies and was last updated 4 years ago.
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
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
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
QuantNeeds
Junior Poster in Training
96 posts since May 2008
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0