【VS】以VS为IDE的报错#?may be unsafe

问题

        刚开始使用VS时,我们写好的代码中含有使用到了<stdio.h>中的scanf()函数后,编译会失败,并且报错:


       1>D:\编程\codes\project code.c\blog c\scanf and scanf_s\scanf and scanf_s\test.c(6,2): error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

        就是:

        1.scanf函数不安全

        2.推荐使用scanf_s来代替scanf函数

        3.如果执意要用scanf函数,并且忽略这条警告,用_CRT_SECURE_NO_WARNINGS 来实现。

出现错误的原因

        我们输入的函数是不安全的。例如在通常使用“scanf”时,我们并没有对将要放入的数据所占用的空间进行检测(可能出现数据越界),因此这样是不够安全的。

        假设创建一个字符数组,它的长度是5,通过scanf输入字符串,代码如下:

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
	char arr[5];
	scanf("%s", arr);
	printf("%s", arr);
	return 0;
}

        仔细想一下,报错只是提醒我们在程序运行之前要进行Debug,如果我们强制使用scanf函数,这时候需要在C源文件第一行加上:

#define _CRT_SECURE_NO_WARNINGS 1

运行,输入 abcd,运行,没有报错;如果输入abcde,那么就程序崩溃了;

(原因在于字符串末尾的结束标志‘\0’也需要一个字符单位存储,输入5个字符就会数组越界) 

        报错是提醒;不要等到程序实际运行崩溃然后 后悔莫及。

        程序崩溃是结果,也就是scanf函数不安全的原因。

类似错误

         除了”scanf“以为,“strcpy”、“strcat”、“sscanf”、“fopen”…等函数都会出现安全检查错误。


strcpy:(string copy)
        Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).

        将源指向的 C 字符串复制到目标指向的数组中,包括终止 null 字符(并在该点处停止)。

        To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.
        为避免溢出,目标指向的数组的大小应足够长,以包含与源相同的 C 字符串(包括终止 null 字符),并且不应在内存中与源重叠。


strcat:(string Concatenate
        Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatenation of both in destination.

        将源字符串的副本追加到目标字符串。destination 中的终止 null 字符被 source 的第一个字符覆盖,并且 null 字符包含在由 destination 中两者的串联形成的新字符串的末尾。

destination and source shall not overlap.

目的地和来源不得重叠。

        目标字符串应该足够长

等等... 


解决方法

一,使用推荐函数scanf_s

scanf_s的使用

        scanf_s是VS提供的函数,它比scanf函数多一个参数:

e.g.1

e.g.2

e.g.3

 

         当占位符是字符和占位符是整型的时候,输出格式是不同的:

        (字符的大小紧跟在地址之后,整形的大小按照顺序放在末尾)

 

        由于scanf_s是VS提供的函数,它在其他编译器上不能使用.  

总结

       

        a.scanf_s函数虽然安全性比较高,但是可移植性差。比如含有scanf_s函数的代码不一定在其它的编译器上能够跑过;

        b.scanf函数虽然没有scanf_s函数安全,但可移植性是比scanf_s强;

二,#define _CRT_SECURE_NO_WARNINGS 1

1.每次使用前加上代码
#define _CRT_SECURE_NO_WARNINGS 1

        我们可以在源文件第一行加一行以上语句,就可以了。

        当我们编译错误时,会出现这条语句,只要把它复制粘贴过来就好。

2.将文件 newc++file 中加上这句代码

        为了找到这个文件,你可以装上这个软件( Everything ),它可以帮你搜索计算机上某一个名称的文件.

        其实VS创建新项目就是复制了一份已经保存好的copy,这个copy就是 newc++file 文件,只要我们把这个copy内加上上面这段代码,以后创建就 省去 手动加上的 操作了。


        Everything 官网icon-default.png?t=N7T8https://www.voidtools.com/zh-cn/downloads/

        (这样就很好找了)

 希望本文可以帮你解决一些问题


完~

未经作者同意禁止转载

猜你喜欢

转载自blog.csdn.net/2301_79465388/article/details/133800062