简体   繁体   English

GDB调试器的分段错误-C

[英]Segmentation fault with GDB debugger - C

I am trying to "debug" this program using GDB debugger. 我正在尝试使用GDB调试器“调试”该程序。 I get the Segmentation fault (core dumped) when I execute the program. 执行程序时出现分段错误(核心已转储)。 This is my first time using GDB, so I do not really know what command to use or what to expect. 这是我第一次使用GDB,所以我真的不知道要使用什么命令或期望什么。

EDIT: I know what the error is. 编辑:我知道什么是错误。 I need to find it using the GDB Debugger 我需要使用GDB调试器找到它

This is the code: 这是代码:

#include <stdio.h>

int main()
{
    int n, i;
    unsigned long long factorial = 1;

    printf("Introduzca un entero: ");
    scanf("%d",n);

    if (n < 0)
        printf("Error! Factorial de un numero negativo no existe.");

    else
    {
        for(i=0; i<=n; ++i)
        {
            factorial *= i;
        }
        printf("Factorial de %d = %llu", n, factorial);
    }

    return 0;
}

Here is the problem: 这是问题所在:

scanf("%d",n);

As you wrote, n is declared as a variable of type int. 如您所写,n被声明为int类型的变量。 What you want to do is to pass the address of n instead of n itself into the function. 您要做的是将n的地址而不是n本身传递给函数。

scanf("%d", &n);

To better understand the implementation of scanf(), check out stdio.h. 为了更好地了解scanf()的实现,请查看stdio.h。

Also, set n = 1. Or otherwise the variable factorial will remain 0 regardless how many loops you've gone through. 另外,设置n =1。否则,无论您经历了多少循环,变量阶乘都将保持为0。

EDIT: what you are trying to do to is to access a memory location passed in by the user, which is highly likely to map to a memory location that belongs to a completely different process or even OS. 编辑:您想要做的是访问用户传递的内存位置,该位置很可能映射到属于完全不同的进程甚至OS的内存位置。 The segmentation fault is generated simply because the location is not accessible. 仅由于无法访问位置而产生分段错误。 What you can do in gdb is using bt in the gdb to a stack trace of segmentation fault. 您可以在gdb中执行的操作是在gdb中使用bt来跟踪分段错误的堆栈跟踪。

I know what the error is. 我知道错误是什么。 I need to find it using the GDB Debugger 我需要使用GDB调试器找到它

You need to read the documentation of gdb (and you should compile your source code with all warnings and debug info, eg gcc -Wall -Wextra -g with GCC ; this puts DWARF debug information inside your executable). 您需要阅读gdb文档 (并且应该使用所有警告和调试信息来编译源代码,例如,带有GCC的 gcc -Wall -Wextra -g ;这会将DWARF调试信息放入可执行文件中)。

The GDB user manual contains a Sample GDB session section. GDB用户手册包含示例GDB会话部分。 You should read it carefully, and experiment gdb in your terminal. 您应该仔细阅读,并在终端中尝试gdb The debugger will help you to run your program step by step, and to query its state (and also to analyze core dumps post-mortem). 调试器将帮助您逐步运行程序,并查询程序的状态(并在事后分析core转储 )。 Thus, you will understand what is happening. 这样,您将了解发生了什么。

Don't expect us to repeat what is in that tutorial section. 不要指望我们重复该教程部分中的内容。

Try also the gdb -tui option. 也尝试使用gdb -tui选项。

PS. PS。 Don't expect StackOverflow to tell you what is easily and well documented. 不要指望StackOverflow会告诉您什么是容易且有据可查的。 You are expected to find and read documentation before asking on SO. 您应该在询问SO之前找到并阅读文档。

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

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