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

Not understanding why I'm getting error message

本文关键字:错误 消息 明白 为什么      更新时间:2023-10-16

我编写了这个程序,在来到这里之前,我已经看了两个小时了。我收到错误消息函数或变量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;
 }

strcpy_s是strcpy的一个版本,具有CRT中"安全增强"中所述的安全增强功能。

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

或者,使用std::string

标准库中的Hi-strcpy不检查要复制的缓冲区是否大于目标缓冲区,而strcpy_s使用size_t num.Elements执行此检查。示例

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

这是一个特定于视觉工作室的警告。来自MSDN:

函数strcpy被认为是不安全的,因为存在无边界检查,并可能导致缓冲区溢出。

因此,正如错误描述中所建议的那样,您可以使用strcpy_s而不是strcpy:

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

MSDN论坛关于主题的帖子

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

这是Visual Studio发出的警告(警告C4996),您可以参考MSDN或谷歌上的警告通知。

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

正如您所知,strcpy的实现是将字符串从source复制到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