【问题标题】:What is a segmentation fault? And how can I fix it? [closed]什么是分段错误?我该如何解决? [关闭]
【发布时间】:2016-03-18 10:42:14
【问题描述】:

输出字符串后,例如20.58,我尝试使用boost::lexical_cast 将其转换为双精度。但是下一行代码没有运行,我的程序结束了,我得到了一个分段错误。

{
    string temp = matches[1];
    int size = temp.find_first_of("<"); //number of chars until "<"
    temp.resize(size);
    cout << "Match: " << temp << "\n";
    Price[1] = boost::lexical_cast<double>(temp);
    cout << "Price: $" << Price[1] << '\n';
    //break;
}

输出:

Match: 20.96

RUN FINISHED; Segmentation fault; real time: 860ms; user: 0ms; system: 0ms

我更喜欢使用 std:stod;但我在 Mac OSX 10.6.8 上使用 netbeans,发现不支持 C++11。

【问题讨论】:

标签: c++ c++11 memory-management segmentation-fault c++98


【解决方案1】:

分段错误是未定义行为的可能结果。

在 *nix 平台上,当程序执行未经授权的内存操作时,例如读取或写入不属于它的内存,系统可能会向此进程发送SIGSEGV 信号,默认情况下会终止它。

在大多数情况下,分段错误是内存管理不善的结果,例如取消引用空指针或悬空指针、在数组外读取/写入,

在您的情况下,故障可能在于 Price[1] 的存在,或者如果存在,则可能是 typeof(Price[0])::operator=(double) 的问题。但我们不会调试您的代码,除非它是 Minimal, Complete, and Verifiable example

【讨论】:

    猜你喜欢
    • 2021-11-14
    • 2019-08-28
    • 2020-03-30
    • 2015-03-23
    • 2014-09-05
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    相关资源
    最近更新 更多