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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:42:35  来源:igfitidea点击:

This function or variable may be unsafe visual studio

c++visual-studio-2012

提问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

localtimeis 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_swas invented by Microsoft, which takes a pointer to a struct tmallocated by you
struct 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) 编译器给出了这个错误。摆脱这个问题很简单。

  1. Go to your VS context menu Project>Properties.
  2. Click Configuration>Properties>C/C++>Preprocessor.
  3. Edit Preprocessor Definitionsand add _CRT_SECURE_NO_WARNINGSlast empty line.
  1. 转到您的 VS 上下文菜单Project>Properties
  2. 单击配置>属性>C/C++>预处理器
  3. 编辑预处理器定义并添加_CRT_SECURE_NO_WARNINGS最后一个空行。

This compile warning will be gone.

此编译警告将消失。

回答by doctorlove

unsafeis 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_WARNINGSif you don't want lots of warnings, for example

例如,
在 C++ 中,visual studio 会抱怨它认为不安全的函数,并建议你#define _CRT_SECURE_NO_WARNINGS如果你不想要很多警告

localtimemight 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);

}