question

LisaSettevig-7958 avatar image
0 Votes"
LisaSettevig-7958 asked LisaSettevig-7958 answered

Error messages.

I'm getting the following message 'StrType3\StrType3.cpp(19,5): error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.' And with 'strcat'. I have the following includes in the program <string> & <cstring>. I've entered this program from a book - 'C++ Primer Plus, 6th edition.'

Thank you.

c++vs-general
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

While the more secure "_s" versions of CRT string functions are recommended to help avoid buffer overruns if the sample code from your book uses the old functions simply changing function names in the book's code to use the secure versions may not be enough for the code from the book to successfully compile. The CRT headers include overloads for the secure versions so that it is not necessary to explicitly pass the size of the receiving buffer to the function if the buffer is declared as an array (e.g., char buf[10]). On the other hand, if the receiving buffer is a pointer (e.g., char*) then the size of the buffer must be explicitly passed to the secure versions.

So while the secure versions are recommended for good reason, using _CRT_SECURE_NO_WARNINGS avoids the need to make any changes to the book's sample code.

0 Votes 0 ·
JeanineZhang-MSFT avatar image
0 Votes"
JeanineZhang-MSFT answered JeanineZhang-MSFT commented

Hi,

I suggest you could refer to the Doc: Compiler Warning (level 3) C4996

To fix a C4996 issue, we usually recommend you change your code. Use the suggested functions and global variables instead. If you need to use the existing functions or variables for portability reasons, you can turn off the warning.

According to the error message, I suggest you could try to use strcpy_s instead of strcpy.

You could also try to turn off deprecation warnings for these functions in the CRT via define _CRT_SECURE_NO_WARNINGS.

For more details I suggest you could refer to the link: https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-3-c4996?view=msvc-160#unsafe-crt-library-functions

Best Regards,

Jeanine


If the response is helpful, please click "Accept Answer" and upvote it.

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@LisaSettevig-7958

May I know if you have got any chance to check my answer? I am glad to help if you have any other questions.

0 Votes 0 ·
RLWA32-6355 avatar image
0 Votes"
RLWA32-6355 answered

Well, the error message gave you the solution. Insert #define _CRT_SECURE_NO_WARNINGS in your source file prior to including any header files.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LisaSettevig-7958 avatar image
0 Votes"
LisaSettevig-7958 answered

Thanks for your help.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.