简体   繁体   English

C ++中getline的分段错误

[英]Segmentation fault with getline in C++

I'm working on an assignment for my programming class. 我正在为我的编程课做作业。 One of the given function prototypes is: 给定的函数原型之一是:

string read_text(const string & prompt);

I wrote the function definition as: 我把函数定义写成:

string read_text(const string & prompt)
{
    cout << "Enter your text: ";
    getline(cin, prompt);
}

However, this gives me an error: 但是,这给了我一个错误:

freq.cpp: In function 'std::string read_text(const std::string&)':
freq.cpp:80: error: no matching function for call to 'getline(std::istream&, const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'

I read that getline is not supposed to refer to a const variable, so I removed that part, but now I get a Segmentation fault when I run the program. 我读到getline不应该引用const变量,所以我删除了那部分,但是现在我在运行程序时遇到了Segmentation错误。

It seems you are confusing a compile-time error with a segmentation fault: these are two rather different things. 您似乎将编译时错误与分段错误混淆:这是两个相当不同的事情。 Compile-time errors happen when the code violates language rules. 当代码违反语言规则时,就会发生编译时错误。 In your case you try to read values into a constant string ( const std::string const& ). 在您的情况下,您尝试将值读入常量字符串( const std::string const& )。 This is clearly not supposed to work. 这显然不应该起作用。 You rather want to use 你宁愿用

std::string read_text()
{
    std::string prompt;
    std::cout << "Enter your text: ";
    std::getline(std::cin, prompt);
    return prompt;
}

Given that input has a fair chance to fail, that's probably not a really good idea as it seems this interface unconditionally succeeds. 鉴于输入有很大的失败机会,这可能不是一个好主意,因为这个接口似乎无条件地成功。

A segmentation fault , on the other hand, is a run-time error. 另一方面, 分段错误是运行时错误。 More specifically, you get a segmentation fault when a piece of memory is accessed which isn't mapped at the accessed location with appropriate access rights. 更具体地说,当访问一块内存时,如果没有映射到具有适当访问权限的访问位置,则会出现分段错误。 Typically, there is no memory at all mapped at the accessed location. 通常,在所访问的位置处根本没有映射存储器。 For example, if you try to dereference a null-pointer and write to it you typically get a segmentation fault. 例如,如果您尝试取消引用空指针并向其写入,则通常会出现分段错误。 Since the behavior of dereference a null-pointer is formally undefined, there is actually no guarantee that anything specific happens. 由于取消引用的行为正式未定义,因此实际上无法保证发生任何特定事件。

The code without the const fails to return a value, ie, there is another form undefined behavior: not returning a value from a non- void function is undefined behavior. 没有const的代码无法返回值,即,还有另一种形式的未定义行为:不从非void函数返回值是未定义的行为。 The compiler probably injected code which tries to build a std::string from the content of some register or memory location and that doesn't seem to contain a valid pointer. 编译器可能会注入代码,这些代码试图从某些寄存器或内存位置的内容构建一个std::string ,但似乎不包含有效指针。

You are missing a return value. 您缺少返回值。 Make it: 做了:

string read_text()
{
    string prompt
    cout << "Enter your text: ";
    getline(cin, prompt);
    return prompt;
}

Disclaimer: No error checks 免责声明:无错误检查

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM