繁体   English   中英

不明白为什么我收到错误消息

[英]Not understanding why I'm getting error message

我编写了该程序的代码,在来到这里之前的两个小时里一直在寻找它。 我收到错误消息函数或变量不安全,请考虑使用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;

 }

strcpy_sstrcpy一个版本,具有CRT的“安全性增强”中所述的安全性增强

strcpy是“不安全的”,因为它可能导致缓冲区溢出 恶意用户可以利用此漏洞来控制您的程序。 因此,Microsoft引入了strcpy_s(称为“安全的字符串复制”),因此不建议使用strcpy 您应该在Visual Studio中使用strcpy_s而不是strcpy

或者,使用std :: string

嗨,标准库中的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

函数或变量不安全,请考虑使用strcpy_s

这是Visual Studio的特定警告。 从MSDN:

由于没有边界检查的事实,函数strcpy被认为是不安全的,并且可能导致缓冲区溢出。

因此,如错误描述中所建议,您可以使用strcpy_s代替strcpy:

strcpy_s(char * strDestination,size_t numberOfElements,const char * strSource);

有关该主题的MSDN论坛帖子

函数或变量不安全,请考虑使用strcpy_s

这是Visual Studio发出的警告(警告C4996),您可以向MSDN寻求此警告,也可以向Google参考此警告通知。

有关编译器警告(级别3)C4996的MSDN链接: http : //msdn.microsoft.com/zh-cn/library/ttcz0bys.aspx

如您所知,strcpy的实现是将字符串从源复制到dest。 并且有一个条件是源比目标大。 并且在这种情况下,函数strcpy无法报告与此有关的任何错误。 这称为“缓冲区溢出”。 因此,在Visual Studio中,M $建议不要使用此函数,建议您使用strcpy_s替换不推荐使用的函数strcpy。

缓冲区溢出参考: http : //en.wikipedia.org/wiki/Buffer_overflow

strcpy_s函数说明: http : //msdn.microsoft.com/zh-cn/library/td1esda9( v=vs.90) .aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM