Start New Discussion within our Software Development Community


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 =;
    int length = int(toolNameString.size());
    length = ( length < 20 ? length : 19 );
    strncpy( toolName, toolNameValue, length);
    toolName[ length ] = '\0'; // append null character to lastName

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).

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.

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

Good answer.

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.

This question has already been answered. Start a new discussion instead.