C++;溢出缓冲区 我在C++中有char命令缓冲区溢出,因为我是C++的新手。这是我的密码。我的问题在第七行 #include "stdafx.h" #include <iostream> #include <cstdlib> int main() { char word[90]; std::cout << "Type in your name to find out your gangster name!" << std::endl; std::cin >> word; std::cout << "Your gangster name is..." << std::endl; std::cout << "Da" << word << std::endl; system("pause"); } #包括“stdafx.h” #包括 #包括 int main() { 字符字[90]; std::cout单词; std::cout

C++;溢出缓冲区 我在C++中有char命令缓冲区溢出,因为我是C++的新手。这是我的密码。我的问题在第七行 #include "stdafx.h" #include <iostream> #include <cstdlib> int main() { char word[90]; std::cout << "Type in your name to find out your gangster name!" << std::endl; std::cin >> word; std::cout << "Your gangster name is..." << std::endl; std::cout << "Da" << word << std::endl; system("pause"); } #包括“stdafx.h” #包括 #包括 int main() { 字符字[90]; std::cout单词; std::cout,c++,visual-studio,C++,Visual Studio,在这种情况下,应该使用std::string和std::getline std::string word; std::getline(std::cin, word); 为什么不把char-word[90]改成std::string-wordchar不是一个“命令”。为什么人们甚至使用char-word?这似乎不安全。@AlexSummers:他们没有。是的。@AlexSummers如果你深入研究,你会发现大部分(全部?)是这样的std::string的实现使用char数组来存储它们的信息。st

在这种情况下,应该使用
std::string
std::getline

std::string word;
std::getline(std::cin, word);

为什么不把char-word[90]改成
std::string-word
char
不是一个“命令”。为什么人们甚至使用char-word?这似乎不安全。@AlexSummers:他们没有。是的。@AlexSummers如果你深入研究,你会发现大部分(全部?)是这样的
std::string
的实现使用
char
数组来存储它们的信息。
std::string
只是在幕后隐藏所有内存管理。我曾参与过一些项目,这些项目禁止在初始化之后使用动态内存分配,以限制内存分段,在这种情况下无法使用de>std::string。