C++ 防止缓冲区溢出
我正在学习缓冲区溢出,想知道防止用户输入超出允许范围的字符并导致缓冲区溢出的最佳方法是什么 防止缓冲区溢出的最佳实践是什么 这是我的密码:C++ 防止缓冲区溢出,c++,buffer-overflow,C++,Buffer Overflow,我正在学习缓冲区溢出,想知道防止用户输入超出允许范围的字符并导致缓冲区溢出的最佳方法是什么 防止缓冲区溢出的最佳实践是什么 这是我的密码: #include <iomanip> #include <iostream> int main() { std::cout << "Buffer Overflow Example" << std::endl; // The user can type more than
#include <iomanip>
#include <iostream>
int main()
{
std::cout << "Buffer Overflow Example" << std::endl;
// The user can type more than 20 characters and overflow the buffer, resulting in account_number being replaced -
// even though it is a constant and the compiler buffer overflow checks are on.
// I need to modify this method to prevent buffer overflow without changing the account_order
// varaible, and its position in the declaration. It must always be directly before the variable used for input.
const std::string account_number = "CharlieBrown42";
char user_input[20];
std::cout << "Enter a value: ";
std::cin >> user_input;
std::cout << "You entered: " << user_input << std::endl;
std::cout << "Account Number = " << account_number << std::endl;
}
#包括
#包括
int main()
{
std::cout防止输入缓冲区溢出的最佳方法是使用不使用固定长度缓冲区的方法。std::cin.getline()是安全使用的一个很好的例子
定义固定长度数组不是C++的方法。如果你在做数组,你真的想考虑你是否使用了最好的方法。
< P>防止输入缓冲区溢出的最好方法是使用不使用固定长度缓冲器的方法。是安全使用的一个很好的例子
定义固定长度数组不是C++的方法。如果你在做一个数组,你真的想考虑你是否使用了最好的方法。< /P> <代码>:ST::CIN。GETLIN(USER输入,20);< /Cord>?因为std::string避免了缓冲区溢出问题,并且在各个方面都更好。在许多情况下也应该避免使用c字符串。std::cin.getline(user_input,20);
?()因为std::string避免了缓冲区溢出问题,并且在各个方面都更好。在许多情况下也应该避免使用c字符串。