GDB 调试器的分段错误 - C [英] Segmentation fault with GDB debugger - C
问题描述
我正在尝试使用 GDB 调试器调试"这个程序.执行程序时出现分段错误(核心转储).这是我第一次使用 GDB,所以我真的不知道要使用什么命令或期望什么.
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.
我知道错误是什么.我需要使用 GDB 调试器找到它
I know what the error is. I need to find it using the GDB Debugger
这是代码:
#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;
}
推荐答案
我知道错误是什么.我需要使用 GDB 调试器找到它
I know what the error is. I need to find it using the GDB Debugger
您需要阅读 gdb
的文档(并且您应该使用所有警告和调试信息来编译您的源代码,例如 gcc -Wall -Wextra -g
和 GCC; 这使得 DWARF 可执行文件中的调试信息).
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).
GDB 用户手册包含 示例 GDB 会话 部分.你应该仔细阅读它,并在你的终端中试验 gdb
.debugger 将帮助您逐步运行程序,并查询其状态(以及还分析 core
dumps 事后).因此,您将了解正在发生的事情.
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.
不要指望我们重复该教程部分的内容.
也试试 gdb -tui
选项.
PS.不要指望 StackOverflow 会告诉你什么是容易且有据可查的.在询问 SO 之前,您应该找到并阅读文档.
这篇关于GDB 调试器的分段错误 - C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!