locked
error C4996: 'strcpy': This function or variable may be unsafe. RRS feed

  • Question

  • Hi !

    i'm getting this error while debugging my code:

     error C4996: 'strcpy': This function or variable may be unsafe.

    Those are parts of my code :

    header file:

    #include <iostream>
    #ifndef NEWSTRING_H_
    #define NEWSTRING_H_
    
    class newstring
    {
    private:
    	char *objekti;
    	int hapsira;
    	static int numri;
    public: 
    	newstring(const char *k);
    	newstring();
    	 ~newstring();
    	 friend  std::ostream &operator<<(std::ostream &os, const newstring &n);
    };
    #endif

    c++ file:

    ...

    int newstring::numri=0;
    newstring::newstring(const char *k)
    {
    	hapsira=strlen(k);
    	objekti= new char[hapsira+1];
    	strcpy(objekti,k);// here is the ERROR line
    	numri++;
    	std::cout<<numri<<" objekte "<<objekti<<" jane krijuar"<<std::endl;
    
    }

    ...

    There is another warning shown by MS visual c++ runtime library , saying that:

    Debug assertion failed!

    Expression: (L"Buffer is too small"&&0).

    How can i deal with this error? I use VS 2012

    Thank you!


    Admir

    Saturday, November 23, 2013 9:56 PM

Answers

  • i'm getting this error while debugging my code:
     error C4996: 'strcpy': This function or variable may be unsafe.

    That's a compilation error. It's normally a warning, so presumably
    you've got the /WX option set to treat all warnings as errors.
    Read the documentation on the warning:
    http://msdn.microsoft.com/en-us/library/ttcz0bys.aspx
    If you have to use strcpy, you can supress the warning using
    _CRT_SECURE_NO_WARNINGS - again, see the documentation.

    There is another warning shown by MS visual c++ runtime library , saying that:
    Debug assertion failed!
    Expression: (L"Buffer is too small"&&0).

    Do yourself a favour - don't bother writing your own string class, use
    the std string class.

    Dave

    • Marked as answer by admiri92 Sunday, November 24, 2013 8:53 PM
    Sunday, November 24, 2013 12:28 AM

All replies

  • i'm getting this error while debugging my code:
     error C4996: 'strcpy': This function or variable may be unsafe.

    That's a compilation error. It's normally a warning, so presumably
    you've got the /WX option set to treat all warnings as errors.
    Read the documentation on the warning:
    http://msdn.microsoft.com/en-us/library/ttcz0bys.aspx
    If you have to use strcpy, you can supress the warning using
    _CRT_SECURE_NO_WARNINGS - again, see the documentation.

    There is another warning shown by MS visual c++ runtime library , saying that:
    Debug assertion failed!
    Expression: (L"Buffer is too small"&&0).

    Do yourself a favour - don't bother writing your own string class, use
    the std string class.

    Dave

    • Marked as answer by admiri92 Sunday, November 24, 2013 8:53 PM
    Sunday, November 24, 2013 12:28 AM
  • Debug assertion failed!

    Expression: (L"Buffer is too small"&&0).

    Could not reproduce this with 2012 express.

    -- pa

    Sunday, November 24, 2013 1:08 AM