C++ 'Strcpy' Gives a Warning (C4996)

C++ 'strcpy' gives a Warning (C4996)

This function (strcpy) is considered unsafe due to the fact that there is no bounds checking and can lead to buffer overflow. (Actually strcpy is infamous for overflow exploits and all programmers avoid it-or at least should avoid it). The advice is to use a safe function which takes into account the size of the destination buffer to avoid overflow. You could also use strncpy (BUT with caution!). There is no problem with your code, i.e. the functions will run as you say but try giving as input a buffer that is larger than the destination buffer. The function will overflow the destination buffer. Check this also link text

C4996 (function unsafe) warning for strcpy but not for memcpy

In general, to compile C code you need a conforming C compiler. Visual Studio is a non-conforming C++ compiler.

You get the warning because Visual Studio is bad. See this.

C4996 appears whenever you use a function that Microsoft regards as obsolete. Apparently, Microsoft has decided that they should dictate the future of the C language, rather than the ISO C working group. Thus you get false warnings for perfectly fine code. The compiler is the problem.

There is nothing wrong with the strcpy() function, that's a myth. This function has existed for some 30-40 years and every little bit of it is properly documented. So what the function does and what it does not should not come as a surprise, even to beginner C programmers.

What strcpy does and does not:

  • It copies a null-terminated string into another memory location.
  • It does not take any responsibility for error handling.
  • It does not fix bugs in the caller application.
  • It does not take any responsibility for educating C programmers.

Because of the last remark above, you must know the following before calling strcpy:

  • If you pass a string of unknown length to strcpy, without checking its length in advance, you have a bug in the caller application.
  • If you pass some chunk of data which does not end with \0, you have a bug in the caller application.
  • If you pass two pointers to strcpy(), which point at memory locations that overlap, you invoke undefined behavior. Meaning you have a bug in the caller application.

For example, in the code you posted, you never initialized the arrays, so your program will likely crash and burn. That bug isn't in the slightest related to the strcpy() function and will not be solved by swapping out strcpy() for something else.

How to fix compile error This function or variable may be unsafe (strcpy)

You get this warning when you use any of the "unsafe" byte copying functions. It's mostly specific to MSVC.

To fix it, use strcpy_s which requires you to also pass a maximum number of bytes to copy (which should be the size of the destination buffer). This prevents buffer overflows.

strcpy_s(chArray, phrase.size()+1, phrase.c_str());

That said, it's easier to use std::string for all this in C++

strcpy() error in Visual studio 2012

There's an explanation and solution for this on MSDN:

The function strcpy is considered unsafe due to the fact that there is
no bounds checking and can lead to buffer overflow.

Consequently, as it suggests in the error description, you can use
strcpy_s instead of strcpy:

strcpy_s( char *strDestination, size_t numberOfElements,

const char *strSource );

and:

To disable deprecation, use
_CRT_SECURE_NO_WARNINGS. See online help for details.

http://social.msdn.microsoft.com/Forums/da-DK/vcgeneral/thread/c7489eef-b391-4faa-bf77-b824e9e8f7d2

error C4996: 'scanf': This function or variable may be unsafe in c programming

It sounds like it's just a compiler warning.

Usage of scanf_s prevents possible buffer overflow.

See: http://code.wikia.com/wiki/Scanf_s

Good explanation as to why scanf can be dangerous: Disadvantages of scanf

So as suggested, you can try replacing scanf with scanf_s or disable the compiler warning.

warning C4018, error C4996 and error C4716 C++

In functions getNumberOfWords and getNumberOfSentences you are comparing an object of signed int with an object of some unsigned integral type

for (int i = 0; i<(this->myData).length(); i++){

The correct statement will look as

for ( std::string::size_type i = 0; i < this->myData.length(); i++){

Operator << is declared as having return type ostream&

ostream& operator << (ostream& outs, const SpeechAnalyst &sa){

but in the function body you are returning nothing.

As for the error relative to using function strcpy you should simply know that in Microsoft there are many idiots that try to make the life of programmers harder. Define name

#define _CRT_SECURE_NO_WARNINGS

before including headers as it is written in the error message.



Related Topics



Leave a reply



Submit