试图进入字符串时的分割故障
segmentation fault when trying to cin into a string?
hi需要此分割故障的帮助,不知道为什么我得到它
Movie *newMovie = (Movie*) malloc(sizeof(Movie));
cout << "nEnter the next movie title: ";
cin >> newMovie->title;
class Movie {
public:
Movie();
std::string title;
int year;
GenreType genre;
};
我检查了DGB并在CIN线上有任何建议吗?btw标题是电影类型的实例,是std :: string
除非您真的知道需要它,否则不要在C 中使用malloc
(提示:不)。
malloc
分配内存,但它不调用任何构造函数 - 它只会为您所看到的一个字节提供大量字节。假装这些字节中有一个对象,当没有构造时,这些字节不起作用。
这样做:
Movie *newMovie = new Movie();
cout << "nEnter the next movie title: ";
cin >> newMovie->title;
您首先需要动态分配吗?为什么不只是:
Movie newMovie;
cout << "nEnter the next movie title: ";
cin >> newMovie.title;
简单的AMD安全方法不使用动态分配:
Movie newMovie;
cout << "nEnter the next movie title: ";
cin >> newMovie.title;
Movie newMovie;
cout << "nEnter the next movie title ";
cin >> newMovie.title;
将做技巧
相关文章:
- 分割故障C++矩阵
- C ,二进制树的高度,而不是检查我的子树是否为空,而是在检查我的子树是否是叶子节点.抛出分割故障
- 我正在为此代码分割故障
- 在执行strcpy时获取分割故障错误
- 空隙指针分割故障
- Xcode会产生分割故障,而不是例外
- OPENCV C 中的分割故障误差
- 分割故障:核心倾倒C 矢量对字符串:
- C 2D数组导致分割故障(核心倾倒)
- 分割故障/错误读数变量-C 列表
- DLSYM导致分割故障
- 使用C 中的操作员删除的分割故障
- 使用Linux X64下的Libmozjs-52(SpidermonKey)进行分割故障
- 使用长变量时分割故障(11)错误
- C - 返回字符串时的分割故障
- 创建VertexArray时的分割故障
- 向量分割故障的向量
- 从std :: fileSystem ::路径对象的类中退出时的分割故障
- C 矢量分割故障
- 基于向量的数据格式的分割故障误差