GDB调试器的分段错误-C [英] Segmentation fault with GDB debugger - C

查看:99
本文介绍了GDB调试器的分段错误-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用GDB调试器调试该程序。执行程序时出现分段错误(核心已转储)。
这是我第一次使用GDB,所以我真的不知道要使用什么命令或期望什么。



编辑:我知道错误是什么。我需要使用GDB调试器找到它



这是代码:

  #include< stdio.h> 

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

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

if(n< 0)
printf( Error!Factorial de un numero negativo no不存在。);

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

返回0;
}


解决方案


我知道错误是什么。我需要使用GDB调试器找到它


您需要阅读 gdb 的文档(并且您应该编译源代码带有所有警告和调试信息的代码,例如 gcc -Wall -Wextra -g GCC ;这会将 DWARF 调试信息放入可执行文件中。 / p>

GDB用户手册包含示例GDB会话部分。您应该仔细阅读,并在终端机中尝试 gdb 调试器将帮助您逐步运行程序,并查询程序的状态(以及还可以分析 核心转储验尸)。因此,您将了解发生了什么。



不要期望我们重复该教程部分中的内容。



也尝试使用 gdb -tui 选项。



PS 。不要指望StackOverflow会告诉您什么是容易且有据可查的。您应该先找到并阅读文档,然后再提出要求。


I am trying to "debug" this program using GDB debugger. 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.

EDIT: I know what the error is. I need to find it using the GDB Debugger

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;
}

解决方案

I know what the error is. I need to find it using the GDB Debugger

You need to read the documentation of gdb (and you should compile your source code with all warnings and debug info, e.g. gcc -Wall -Wextra -g with GCC; this puts DWARF debug information inside your executable).

The GDB user manual contains a Sample GDB session section. You should read it carefully, and experiment gdb in your terminal. 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). 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.

PS. Don't expect StackOverflow to tell you what is easily and well documented. You are expected to find and read documentation before asking on SO.

这篇关于GDB调试器的分段错误-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆