简体   繁体   English

编程接收到的信号SIGSEGV,在代码块中调试时出现分段错误

[英]Program received signal SIGSEGV, Segmentation fault while debugging in codeblocks

I'm a beginner in c++ and I was trying to solve https://projecteuler.net/problem=9 . 我是C ++的初学者,我正在尝试解决https://projecteuler.net/problem=9 I wrote a code for it and it shows the error - Program received signal SIGSEGV, Segmentation fault. 我为此写了一个代码,它显示了错误-程序收到信号SIGSEGV,分段错误。 In strcmp () (C:\\Windows\\syswow64\\msvcrt.dll) while debugging. 在调试时在strcmp()(C:\\ Windows \\ syswow64 \\ msvcrt.dll)中。

If I straightaway run the program, a dialog box appears that says "windows is checking for a solution." 如果我直接运行该程序,则会出现一个对话框,显示“ Windows正在检查解决方案”。

I've tried not using the string function and instead of writing pytha(a,b,c)=="true" , I just wrote axa+bxb=c*c (I wrote * instead of x but here it is not showing * between the two a's so I am replacing it with x) and the code works perfectly fine. 我试过不使用字符串函数,而不是写pytha(a,b,c)==“ true”,我只是写了axa + bxb = c * c(我写了*而不是x,但是这里没有显示*在两个a之间,因此我将其替换为x),并且代码运行正常。 But the thing is why does it not work with the string function? 但问题是为什么它不能与字符串函数一起使用?

I do not see anything wrong with the code. 我没有看到代码有什么问题。

I've found plenty of similar questions- 1. https://www.codeproject.com/Questions/93770/what-is-this-means-Program-received-signal-SIGSEGV 我发现了很多类似的问题-1. https://www.codeproject.com/Questions/93770/what-is-this-means-Program-received-signal-SIGSEGV
According to this one, my program is referring to a memory location which it does not have access to. 根据这一点,我的程序引用的是它无法访问的存储位置。 But I do not see anything that is restricting this code to access something. 但是我看不到任何限制此代码访问某些内容的东西。

  1. Program received signal SIGSEGV, Segmentation fault error 程序收到信号SIGSEGV,分段故障错误

3. Debug---Program received signal SIGSEGV, Segmentation fault 3. 调试---编程接收到的信号SIGSEGV,分段错误

  1. program received signal SIGSEGV, segmentation fault 程序收到信号SIGSEGV,分段错误

  2. "Program received signal SIGSEGV, Segmentation fault." “程序收到信号SIGSEGV,分段故障。”

  3. Program received signal SIGSEGV, Segmentation fault 程序收到信号SIGSEGV,分段故障

  4. Program received signal SIGSEGV, segmentation fault, Linked list program 程序收到信号SIGSEGV,分段故障,链表程序

None of them answer my query as I am not able to relate to the codes mentioned in them to my code. 他们中没有一个回答我的查询,因为我无法将其中提到的代码与我的代码相关联。 The link numbered 5 mentions that probably the error is because of the large number of computations involved. 编号为5的链接提到该错误可能是由于涉及大量计算。 Even I had that doubt for my code, but it works fine when I don't use the function "pytha". 即使我对我的代码也有疑问,但是当我不使用“ pytha”函数时,它可以很好地工作。 Also, I do not see the large number of steps involved related in any way to an error related to memory access. 另外,我看不到涉及与内存访问相关的错误的大量步骤。

Also, even if large number of steps are involved is the reason, the program should compile when given enough time. 同样,即使是涉及大量步骤的原因,程序也应在有足够时间的情况下进行编译。 But it doesn't. 但事实并非如此。 It straightaway shows the error that "Windows is looking for a solution." 它清楚地显示了“ Windows正在寻找解决方案”的错误。

#include <cmath>
#include <iostream>
#include <string>

using namespace std;

string pytha(int a, int b, int c) {
    if(a * a + b * b == c * c) return "true";
}

int main() {
    for(int a = 1; a < 1000; a++) {
        for(int b = 1; b < 1000; b++) {
            for(int c = 1; c < 1000; c++) {
                if(a + b + c == 1000) {
                    if(pytha(a, b, c) == "true")
                        cout << "a= " << a << " b= " << b << " c= " << c;
                }
            }
        }
    }
}

Please note that this code is a very inefficient one. 请注意,这段代码效率很低。 The point is not to solve the question but to know why is the program not compiling. 关键不是要解决问题,而是要知道为什么程序无法编译。

pytha doesn't return a value on its all control flow paths. pytha不会在其所有控制流路径上返回值。

Fix: 固定:

string pytha(int a, int b, int c)
{
    if (a*a+b*b==c*c)
        return "true";
    return "";
}

Always compile your code with warnings enabled. 始终在启用警告的情况下编译代码。 For gcc and clang the compiler command line options are -Wall -Wextra -Werror . 对于gccclang ,编译器命令行选项为-Wall -Wextra -Werror


You probably want to use bool type instead of string . 您可能要使用bool类型而不是string

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

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