C++ Visual Studio 2012 中的 strcpy() 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12204116/
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 16:01:12  来源:igfitidea点击:

strcpy() error in Visual studio 2012

c++visual-studio-2012

提问by Ivan Prodanov

so I have this code:

所以我有这个代码:

#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

...

...

char* b = new char [10];
strcpy(b, "1234567890");

error: microsoft visual studio 11.0\vc\include\string.h(110) : see declaration of 'strcpy'

错误:Microsoft Visual Studio 11.0\vc\include\string.h(110):请参阅“strcpy”的声明

How do I fix it?

我如何解决它?

回答by Eat at Joes

A quick fix is to add the _CRT_SECURE_NO_WARNINGS definition to your project's settings

快速修复是将 _CRT_SECURE_NO_WARNINGS 定义添加到您的项目设置中

Right-click your C++ and chose the "Properties" item to get to the properties window.

右键单击您的 C++ 并选择“属性”项以进入属性窗口。

Now follow and expand to, "Configuration Properties"->"C/C++"->"Preprocessor"->"Preprocessor definitions".

现在按照并扩展到“配置属性”->“C/C++”->“预处理器”->“预处理器定义”。

In the "Preprocessor definitions" add

在“预处理器定义”中添加

_CRT_SECURE_NO_WARNINGS

_CRT_SECURE_NO_WARNINGS

but it would be a good idea to add

但最好添加

_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)

_CRT_SECURE_NO_WARNINGS;%(预处理器定义)

as to inherit predefined definitions

至于继承预定义的定义

IMHO & for the most part this is a good approach.

恕我直言,在大多数情况下,这是一个很好的方法。

回答by John Humphreys - w00te

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

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

函数 strcpy 被认为是不安全的,因为没有边界检查并且可能导致缓冲区溢出。

因此,正如错误描述中所建议的那样,您可以使用 strcpy_s 而不是 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.

要禁用弃用,请使用 _CRT_SECURE_NO_WARNINGS。详细信息请参见在线帮助。

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

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

回答by Pat

I had to use strcpy_s and it worked.

我不得不使用 strcpy_s 并且它起作用了。

#include "stdafx.h"
#include<iostream>
#include<string>

using namespace std;

struct student
{
    char name[30];
    int age;
};

int main()
{

    struct student s1;
    char myname[30] = "John";
    strcpy_s (s1.name, strlen(myname) + 1 ,myname );
    s1.age = 21;

    cout << " Name: " << s1.name << " age: " << s1.age << endl;
    return 0;
}

回答by Avi Berger

The message you are getting is advice from MS that they recommend that you do not use the standard strcpy function. Their motivation in this is that it is easy to misuse in bad ways (and the compiler generally can't detect and warn you about such misuse). In your post, you are doing exactly that. You can get rid of the message by telling the compiler to not give you that advice. The serious error in your code would remain, however.

您收到的消息是 MS 的建议,他们建议您不要使用标准的 strcpy 函数。他们这样做的动机是很容易以不好的方式误用(并且编译器通常无法检测并警告您这种误用)。在您的帖子中,您正是这样做的。你可以通过告诉编译器不要给你那个建议来摆脱这个消息。但是,代码中的严重错误仍然存​​在。

You are creating a buffer with room for 10 chars. You are then stuffing 11 chars into it. (Remember the terminating '\0'?) You have taken a box with exactly enough room for 10 eggs and tried to jam 11 eggs into it. What does that get you? Not doing this is your responsibility and the compiler will generally not detect such things.

您正在创建一个可容纳 10 个字符的缓冲区。然后将 11 个字符塞入其中。(还记得结尾的 '\0' 吗?)你拿了一个刚好足够容纳 10 个鸡蛋的盒子,并试图把 11 个鸡蛋塞进去。这对你有什么好处?不这样做是你的责任,编译器通常不会检测到这样的事情。

You have tagged this C++ and included string. I do not know your motivation for using strcpy, but if you use std::string instead of C style strings, you will get boxes that expand to accommodate what you stuff in them.

您已标记此 C++ 并包含字符串。我不知道您使用 strcpy 的动机,但是如果您使用 std::string 而不是 C 样式字符串,您将获得可扩展以容纳您在其中的内容的框。

回答by LifeInKernelSpace

If you are getting an error saying something about deprecated functions, try doing #define _CRT_SECURE_NO_WARNINGSor #define _CRT_SECURE_NO_DEPRECATE. These should fix it. You can also use Microsoft's "secure" functions, if you want.

如果您收到有关已弃用函数的错误信息,请尝试执行#define _CRT_SECURE_NO_WARNINGS#define _CRT_SECURE_NO_DEPRECATE。这些应该修复它。如果需要,您还可以使用 Microsoft 的“安全”功能。

回答by rktuxyn

Add this line top of the header

在标题的顶部添加此行

#pragma warning(disable : 4996)

回答by waleed_wamo

For my problem, I removed the #include <glui.h>statement and it ran without a problem.

对于我的问题,我删除了该 #include <glui.h>语句并且它运行没有问题。