C++ 这个函数或变量可能是不安全的visual studio
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19321804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
This function or variable may be unsafe visual studio
提问by QuentinRM
I got a problem on visual studio. I try to use the localtime function from "time.h".
我在 Visual Studio 上遇到了问题。我尝试使用“time.h”中的 localtime 函数。
Visual studio tells me it's an unsafe function. However, I have tu use this one for my school exercice. I saw that you can disable this unsafe error by going in the project properties, build tab, and check "enable unsafe code".
Visual Studio 告诉我这是一个不安全的功能。但是,我有你在学校练习中使用这个。我看到您可以通过进入项目属性、构建选项卡并选中“启用不安全代码”来禁用此不安全错误。
Nevertheless, I don't have a build tab, as you can see there : http://puu.sh/4NkYC.png
尽管如此,我没有构建选项卡,正如您在那里看到的:http: //puu.sh/4NkYC.png
I'm using windows 7 and visual studio 2012 Ultimate. It looks like the "build tab" and "enable unsafe code" has vanished :/ Maybe you know how to fix that ?
我正在使用 Windows 7 和 Visual Studio 2012 Ultimate。看起来“构建选项卡”和“启用不安全代码”已经消失了:/也许你知道如何解决这个问题?
thank's a lot :)
多谢 :)
回答by AlexMelw
You can switch off the warning using the following directive:
您可以使用以下指令关闭警告:
#pragma warning(disable : 4996) //_CRT_SECURE_NO_WARNINGS
回答by Henno
localtime
is marked unsafe by the MS-Compiler because it returns a pointer to a statically allocated struct tm
. This is obviously a bad idea.
Therefore, localtime_s
was invented by Microsoft, which takes a pointer to a struct tm
allocated by youstruct tm timeinfo;
localtime_s(&timeinfo, &rawtime);
Use this (and have your program be Microsoft specific) or switch off the warning by defining _CRT_SECURE_NO_WARNINGS
.
localtime
被 MS 编译器标记为不安全,因为它返回一个指向静态分配的struct tm
. 这显然是个坏主意。
因此,localtime_s
是由微软发明的,它需要一个指向struct tm
你分配的指针使用这个(并让你的程序是微软特定的)或通过定义关闭警告。struct tm timeinfo;
localtime_s(&timeinfo, &rawtime);_CRT_SECURE_NO_WARNINGS
回答by Umut D.
Visual Studio (VS) compiler gives this error. It's simple to get rid of this problem.
Visual Studio (VS) 编译器给出了这个错误。摆脱这个问题很简单。
- Go to your VS context menu Project>Properties.
- Click Configuration>Properties>C/C++>Preprocessor.
- Edit Preprocessor Definitionsand add _CRT_SECURE_NO_WARNINGSlast empty line.
- 转到您的 VS 上下文菜单Project>Properties。
- 单击配置>属性>C/C++>预处理器。
- 编辑预处理器定义并添加_CRT_SECURE_NO_WARNINGS最后一个空行。
This compile warning will be gone.
此编译警告将消失。
回答by doctorlove
unsafe
is part of C# not C++. For example these docsclearly say
unsafe
是 C# 的一部分,而不是 C++。例如,这些文档清楚地说明
/unsafe (C# Compiler Options)
/unsafe(C# 编译器选项)
at the top
In C++, visual studio will complain about functions it regards as unsecureand suggest you #define _CRT_SECURE_NO_WARNINGS
if you don't want lots of warnings, for example
例如,
在 C++ 中,visual studio 会抱怨它认为不安全的函数,并建议你#define _CRT_SECURE_NO_WARNINGS
如果你不想要很多警告
localtime
might give you the following:
localtime
可能会给你以下信息:
warning C4996: 'localtime': This function or variable may be unsafe. Consider using localtime_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
警告 C4996:“localtime”:此函数或变量可能不安全。考虑使用 localtime_s 代替。要禁用弃用,请使用 _CRT_SECURE_NO_WARNINGS。详细信息请参见在线帮助。
回答by Sreejith Sathyan
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
char counter_str[10];
int main()
{
time_t my_time = time(NULL)// declaring argument of time();
sprintf(counter_str,ctime(&my_time));//fetch current time
printf(counter_str);
}