strcpy() error in Visual studio 2012

后端 未结 7 956
Happy的楠姐
Happy的楠姐 2021-02-06 12:50

so I have this code:

#include \"stdafx.h\"
#include 
#include 
#include 
using namespace std;

...<

相关标签:
7条回答
  • 2021-02-06 13:04

    Add this line top of the header

    #pragma warning(disable : 4996)
    
    0 讨论(0)
  • 2021-02-06 13:07

    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.

    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.

    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.

    0 讨论(0)
  • 2021-02-06 13:09

    I had to use strcpy_s and it worked.

    #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;
    }
    
    0 讨论(0)
  • 2021-02-06 13:16

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

    0 讨论(0)
  • 2021-02-06 13:20

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

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

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

    In the "Preprocessor definitions" add

    _CRT_SECURE_NO_WARNINGS

    but it would be a good idea to add

    _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)

    as to inherit predefined definitions

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

    0 讨论(0)
  • 2021-02-06 13:23

    There's an explanation and solution for this on 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 );

    and:

    To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

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

    0 讨论(0)
提交回复
热议问题