Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jan 1st, 2009, 3:53 PM   #1
everid
Programmer
 
Join Date: Dec 2008
Location: Physicist at North Carolina
Posts: 85
Rep Power: 6 everid is on a distinguished road
error: C4996: 'strcpy': This function or variable may be unsafe.

Thank you for your help.
The error location is indicated in the following codes by: //error here
I'm trying to copy an object to another.

error information:
1>d:\outopc\vcversion\widget_op1\widget_op1\cmessage.h(19) : warning Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

Two files:
1.CMessage.h
c++ Syntax (Toggle Plain Text)
  1. #include<string>
  2. #include<iostream>
  3. using namespace std;
  4. class CMessage{
  5. char *buffer;
  6. public:
  7. CMessage(){
  8. buffer=new char('\0');
  9. }
  10. ~CMessage(){
  11. delete [] buffer;
  12. }
  13. void Display(){
  14. cout<<buffer<<endl;
  15. }
  16. void Set(char *string){
  17. delete [] buffer;
  18. buffer=new char[strlen(string)+1];
  19. strcpy(buffer,string);
  20. }
  21. };

2. main.cpp
c++ Syntax (Toggle Plain Text)
  1. #include"CMessage.h"
  2. int main(){
  3. CMessage c1,c2;
  4. char *pc="hello 2009";
  5. c1.Set(pc);
  6. c1.Display();
  7. c2=c1; //error here
  8. return 0;
  9. }
everid is offline   Reply With Quote
Old Jan 1st, 2009, 4:21 PM   #2
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 330
Rep Power: 7 Jabo is on a distinguished road
Re: error: C4996: 'strcpy': This function or variable may be unsafe.

strcopy is depracated, like the error says, and is considered unsafe, like the error says. Use strcopy_s instead, like the error says. Another alternative is to disable depracation using the macro stated in the error.
Jabo is offline   Reply With Quote
Old Jan 1st, 2009, 4:30 PM   #3
everid
Programmer
 
Join Date: Dec 2008
Location: Physicist at North Carolina
Posts: 85
Rep Power: 6 everid is on a distinguished road
Re: error: C4996: 'strcpy': This function or variable may be unsafe.

First of all thank you.
While, I'm pretty new to C++. I don't know why it is "deprecated"? What's your definition of "depracated" in terms of memory of other computer langauge?
could you please explain more on why I can't copy the object to each other?
Thank you very much.

Quote:
Originally Posted by Jabo View Post
strcopy is depracated, like the error says, and is considered unsafe, like the error says. Use strcopy_s instead, like the error says. Another alternative is to disable depracation using the macro stated in the error.
everid is offline   Reply With Quote
Old Jan 1st, 2009, 4:51 PM   #4
Freaky Chris
Expert Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 771
Rep Power: 7 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
Re: error: C4996: 'strcpy': This function or variable may be unsafe.

Quote:
Originally Posted by everid View Post
First of all thank you.
While, I'm pretty new to C++. I don't know why it is "deprecated"? What's your definition of "depracated" in terms of memory of other computer langauge?
could you please explain more on why I can't copy the object to each other?
Thank you very much.
Something can be depreciated for a number of reasons. It could be anything that was a minor issue with it. So a newer version of the function is released so that the old problems no longer present themselves. All you need to do is use strcpy_s() instead of strcpy()

Chris
__________________
Steven Skiena - Algorithms

,[->+>+<<]>>[-<<+>>]>++++++++[-<++++++++>]<+[-<->]>+<<[[-]+++++++++++++++.[-]>]>>[+++++++++.[-]],
brainf**k -- It's such a pretty language
Freaky Chris is offline   Reply With Quote
Old Jan 1st, 2009, 5:28 PM   #5
everid
Programmer
 
Join Date: Dec 2008
Location: Physicist at North Carolina
Posts: 85
Rep Power: 6 everid is on a distinguished road
Re: error: C4996: 'strcpy': This function or variable may be unsafe.

I did as you told me. I changed nothing by strcpy() to be strcpy_s() which takes three parameters including the size of the string.
It crashed.
The compiling goes fine. But when I try to run it as usual by pressing (ctrl+F5, Visual C++ 2005 express), another message pumped out saying:
"Debug Assertion Failed!"
Then I have to click on "abort" to stop it.
Thank you for any suggestion on this.

Quote:
Originally Posted by Freaky Chris View Post
Something can be depreciated for a number of reasons. It could be anything that was a minor issue with it. So a newer version of the function is released so that the old problems no longer present themselves. All you need to do is use strcpy_s() instead of strcpy()

Chris
everid is offline   Reply With Quote
Old Jan 1st, 2009, 5:31 PM   #6
everid
Programmer
 
Join Date: Dec 2008
Location: Physicist at North Carolina
Posts: 85
Rep Power: 6 everid is on a distinguished road
Re: error: C4996: 'strcpy': This function or variable may be unsafe.

Actually, my guessing is that the problem isn't strcpy() function, but how the operator "=" works. As long as I comment the "c2=c1;" out, everything works find. So very confusing.
everid is offline   Reply With Quote
Old Jan 1st, 2009, 5:36 PM   #7
Klarre
Game engine designer
 
Klarre's Avatar
 
Join Date: May 2005
Location: Sweden
Posts: 349
Rep Power: 10 Klarre is on a distinguished road
Re: error: C4996: 'strcpy': This function or variable may be unsafe.

Then the size is probably bad. Or there might be another problem since you are in the constructor using the new operator to initialize the buffer pointer. But you use the delete[] operator to destroy it. You should use the new[] operator when initializing the object if you are using the delete[] operator.
buffer = new char[1];
buffer[0] = '\0';

And you can still use the strcpy function. strcpy_s will not compile using some compilers.
__________________
http://klarre.se/
Klarre is offline   Reply With Quote
Old Jan 1st, 2009, 6:03 PM   #8
Freaky Chris
Expert Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 771
Rep Power: 7 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
Re: error: C4996: 'strcpy': This function or variable may be unsafe.

Quote:
Originally Posted by Klarre View Post
And you can still use the strcpy function. strcpy_s will not compile using some compilers.
Anything other than microsofts VC++ MFC Compiler
__________________
Steven Skiena - Algorithms

,[->+>+<<]>>[-<<+>>]>++++++++[-<++++++++>]<+[-<->]>+<<[[-]+++++++++++++++.[-]>]>>[+++++++++.[-]],
brainf**k -- It's such a pretty language
Freaky Chris is offline   Reply With Quote
Old Jan 1st, 2009, 6:22 PM   #9
Ancient Dragon
Achieved Level 70

 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 4,345
Rep Power: 10 Ancient Dragon will become famous soon enoughAncient Dragon will become famous soon enough
Re: error: C4996: 'strcpy': This function or variable may be unsafe.

Quote:
Originally Posted by Jabo View Post
strcopy is depracated, like the error says, and is considered unsafe, like the error says. Use strcopy_s instead, like the error says. Another alternative is to disable depracation using the macro stated in the error.
No its not depracated. Only Microsoft 2005/2008 compilers think so, the C and C++ standards have not changed.

Quote:
Originally Posted by Freaky Chris View Post
Anything other than microsofts VC++ MFC Compiler
That's not correct -- on Microsoft compilers such as VC++2008 just use pragma to disable that warning: #pragma warning(disable: 4996)
__________________
PFO's FAQ is here
Forum Rules

There is no cow level (Diablo III)
If you never push yourself you will never improve (Diablo III)
Ancient Dragon is offline   Reply With Quote
Old Jan 1st, 2009, 6:29 PM   #10
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,329
Rep Power: 11 grumpy will become famous soon enough
Re: error: C4996: 'strcpy': This function or variable may be unsafe.

Quote:
Originally Posted by Jabo View Post
strcopy is depracated, like the error says, and is considered unsafe, like the error says. Use strcopy_s instead, like the error says. Another alternative is to disable depracation using the macro stated in the error.
This is wrong on so many levels, it's crazy.

Firstly, deprecation of strcpy() simply means that the function has been flagged for removal from a future version of the standard. The C++ standard has deprecated strcpy() (in fact, most content of the C header <string.h> and C++ header <cstring>). The intent is that std::string will be used instead of shuffling C-style strings (i.e. arrays of char).

strcpy_s() is a Microsoft specific replacement for strcpy(), which supplies an extra argument that is the maximum length of the buffer being copied. In other words, it is functionally equivalent to strncpy(). Microsoft proposed it to ISO for incorporation into a future C++ standard but, as far as I know, will not be incorporated. Unless you intend to stick with Microsoft compilers forever, it is inadvisable to use strcpy_s(). What we have here is a compiler (legitimately) flagging use of a function that is deprecated in the standard, but then encouraging use of a vendor-specific, non-standard, replacement. That is vendor lock-in in a nasty form.

In the actual code
class CMessage{
    char *buffer;
public:
    CMessage(){
        buffer=new char('\0');
    }
    ~CMessage(){
        delete [] buffer;
    }
    void Display(){
        cout<<buffer<<endl;
    }
    void Set(char *string){
        delete [] buffer;
        buffer=new char[strlen(string)+1];
        strcpy(buffer,string);
    }
};
from everid's post, the default constructor allocates a single char using operator new, but the destructor and the Set() function destroy it with operator delete []. That is undefined behaviour. The forms of operator new and operator delete have to match.

Since the class dynamically allocates memory, it is a good idea to supply copy constructor and assignment operators. Otherwise, when copying or assigning an object, the result is two (or more) objects that hold the same memory. That leaves dangling references when any of the copies are destroyed.

Having variable or argument names that are the same as type names is also a good way to confuse yourself. string (albeit within namespace std) is the name of a standard type in the C++ standard library.

Personally, I'd just follow the intent of the standard and do this;
class CMessage{
    std::string buffer;
public:
    CMessage(){
        buffer="";
    }
    ~CMessage(){    // nothing needed
    }
    void Display(){
        cout<<buffer<<endl;
    }
    void Set(const char *s){   // renamed the argument and made it const
        buffer=s;
    }
};
This will work since std::string provides its own copy constructor and assignment operators that work as needed. Personally, I wouldn't bother with the CMessage class in this form at all: easier to just work with std::string directly rather than wrapping it.
grumpy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
change the empty function from the old format to the new format powah Sed and Awk 1 Mar 28th, 2012 2:27 AM
Returning a value from a variable to the main function colt C 4 Jun 20th, 2009 3:26 PM
Assigning a class function as a callback function core8583 C++ 4 Jun 18th, 2008 7:20 PM
Compiling Maverik 6.2 (from C) megamind5005 C 16 May 3rd, 2006 5:41 PM
Can a function give a global variable a value? clanotheduck Python 4 Sep 25th, 2005 1:56 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:50 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC