bod*_*heg 2 c++ gcc segmentation-fault
发现我不明白的奇怪行为:
std::vector<std::string> subdomainVisits(std::vector<std::string> &cpdomains)
{
// return std::vector<std::string>();
}
int main(int argc, char const *argv[])
{
std::vector<std::string> data = { "9001 discuss.leetcode.com" };
auto result = subdomainVisits(data);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,return在subdomainVisits函数中注释会导致分段错误(使用gcc版本7.3.0(Debian 7.3.0-19)).取消注释解决了这个问题.
为什么会这样?
Bat*_*eba 10
您编写的程序行为未定义.
非void函数必须在所有控制路径上具有显式返回值.
唯一的例外是main,这是隐含的return 0;.
相当多的编译器会警告你有关如上所述的微不足道的案例.你没有设置足够高的警告级别吗?(通过-Wall并-Wextra"关闭"gcc上的警告级别.)
请注意,C++标准不要求编译器编译失败:理论计算机科学(停止问题)告诉我们无法证明可达性.
| 归档时间: |
|
| 查看次数: |
103 次 |
| 最近记录: |