我编写了这个程序,在来到这里之前的最后两个小时我一直在寻找它。我收到错误消息函数或变量unsafe考虑使用strcpy_s 我的书中没有提到strcpy_s。也许一副清新的眼睛会看到我看不到的东西。这是我写的代码
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class HotelRoom
{
private:
char room_no [3];
char* guest;
int capacity;
int occupancy;
double rate;
public:
HotelRoom(char room[],int, double = 89.00, char* n_p = 0, int= 0);
~HotelRoom();
void Display_get_number();
void Display_guest();
int get_capacity();
int get_status();
double get_rate();
void change_rate(double);
bool change_status(int);
};
HotelRoom::HotelRoom (char room[], int cap, double rt, char*n_p, int occup)
{
strcpy(room_no, room);
guest = new char[strlen(n_p) + 1];
strcpy(guest, n_p);
capacity = cap;
occupancy = occup;
rate = rt;
cout << " defaut for the following " << room << endl << endl;
cout << " Capacity " << capacity << endl << endl;
cout << " rate " << rate << endl << endl;
cout << " Room Guest " << guest << endl << endl;
cout << " Occupancy " << occupancy << endl << endl;
}
HotelRoom::~HotelRoom()
{
cout << "\nHotelRoom " << room_no <<" terminated. ";
delete [] guest;
}
void HotelRoom::Display_get_number()
{
cout << room_no;
}
void HotelRoom::Display_guest()
{
cout << guest;
}
int HotelRoom::get_capacity()
{
return capacity;
}
int HotelRoom::get_status()
{
return occupancy;
}
double HotelRoom::get_rate()
{
return rate;
}
void HotelRoom::change_rate( double amount)
{
rate += amount;
}
bool HotelRoom::change_status(int occupancy)
{
if (occupancy <= capacity )
{
this-> occupancy = occupancy;
return true;
}
else
return false;
}
int _tmain(int argc, _TCHAR* argv[])
{
cout << setprecision(2)
<< setiosflags(ios::fixed)
<< setiosflags(ios::showpoint);
HotelRoom guest ("134",4,0, "Dennard Beale", 0);
cout << endl;
cout << " *****End of program***** " << endl;
system("pause");
return 0;
}
答案 0 :(得分:1)
strcpy_s是strcpy
的一个版本,其中包含Security Enhancements in the CRT中所述的安全增强功能。
strcpy
“不安全”,因为它可能导致Buffer Overflows。恶意用户exploit this可以控制您的程序。因此,Microsoft引入了strcpy_s(读作'string copy secure'),不推荐使用strcpy
。您应该在Visual Studio中使用strcpy_s
而不是strcpy
。
或者,使用std::string
答案 1 :(得分:1)
标准库中的strcpy不检查要复制的缓冲区是否大于目标缓冲区,而strcpy_s使用size_t numofElements执行此检查。 实施例
char chbuff[10]={0}; //a buffer
strcpy(chbuff,"helloworld!"); //This will probably cause buffer overrun and undefined behaviour
strcpy_s(chbuff,10,"helloworld!"); //this will give you debug assertion failure in visual studio with error buffer too small
答案 2 :(得分:0)
函数或变量unsafe考虑使用strcpy_s
这是视觉工作室特定的警告。来自MSDN:
由于存在这样的事实,strcpy函数被认为是不安全的 没有边界检查,可能导致缓冲区溢出。
因此,正如它在错误描述中所暗示的那样,您可以使用 strcpy_s而不是strcpy:
strcpy_s(char * strDestination,size_t numberOfElements,const char * strSource);
答案 3 :(得分:0)
函数或变量unsafe考虑使用strcpy_s
这是来自Visual Studio的警告(警告C4996),您可以参考MSDN获取此警告或Google此警告通知。
关于编译器警告的MSDN链接(级别3)C4996:http://msdn.microsoft.com/en-us/library/ttcz0bys.aspx
如您所知,strcpy的实现是将字符串从源复制到dest。并且存在源比dest大的条件。在这种情况下,函数strcpy无法报告任何有关此错误。这称为“缓冲区溢出”。因此在Visual Studio中,M $建议不推荐使用此函数,它建议您使用strcpy_s替换已弃用的函数strcpy。
缓冲区溢出引用:http://en.wikipedia.org/wiki/Buffer_overflow
strcpy_s函数说明:http://msdn.microsoft.com/en-us/library/td1esda9(v=vs.90).aspx