不明白为什么我会收到错误消息
Not understanding why I'm getting error message
我编写了这个程序,在来到这里之前,我已经看了两个小时了。我收到错误消息函数或变量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
- C++错误消息*成员参考.**初学者*
- 如何通过参数抛出错误消息
- glad 导致 glfwSwapBuffers 返回错误消息
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- 如何接受 [ENTER] 键作为无效输入并发送错误消息
- 重新定义预定义的 errno 错误消息 (E2BIG)
- SDL 映像:无法打开映像,仅显示错误消息
- 错误消息:使用"string* +="后"no match for 'operator+='"
- 错误消息"expected expression"....有人知道它为什么这么说吗?
- 在 Eclipse: "error: no match for 'operator='" 中获取错误消息
- GCC 警告和 gcc 错误消息之间的区别
- 为 c++ cin 上的输入数无效生成错误消息
- 来自带有mbedtls TLS的Mongoose Web服务器的错误消息
- 如何使用realsense摄像头调试ROS错误消息
- C++ 概念 - 需要括号中的概念会导致 2 条冲突的错误消息
- 编译器收到错误消息(textc.exe已停止)
- 结构数组的构造函数错误,错误消息:没有构造函数实例与参数列表匹配
- 使用 c++/WinRT Direct3D UWP Game DR 模板 Direct X11 的错误消息
- max_element() 给出非常奇怪的错误消息
- 不断收到错误消息,并非所有控制路径都返回值