Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

strcpy() error in Visual studio 2012

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'

How do I fix it?

like image 466
Ivan Prodanov Avatar asked Aug 30 '12 19:08

Ivan Prodanov


2 Answers

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.

like image 83
Eat at Joes Avatar answered Oct 12 '22 12:10

Eat at Joes


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

like image 40
John Humphreys Avatar answered Oct 12 '22 10:10

John Humphreys