找不到分段错误在哪里

发布于 2024-11-03 19:59:48 字数 3190 浏览 1 评论 0原文

我已经编程了一段时间了,没有那么多,但是对我来说找到分段错误在哪里真的很难,已经尝试了几乎所有的东西:(。这是一个学校项目,老师通常不会提供帮助 好吧,这

就是代码:

#include "funcoes2.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

listaa total_aero = NULL;

void guarda_voo()
{
    listaa t; /* struct list of airports */
    voo novo_voo;
    aeroporto novo_aeroporto1;
    aeroporto novo_aeroporto2;


    scanf("%s %s %s %d:%d %s %f\n", novo_voo.codigo, novo_voo.aero_partida, novo_voo.aero_chegada, &novo_voo.hora_partida, &novo_voo.minuto_partida, novo_voo.hora_chegada, &novo_voo.preco);

    for(t=total_aero; t != NULL; t = t->prox){
        if(!strcmp(t->naero.codigo, novo_voo.aero_partida))
            break;
    }

    if(t == NULL){
        insere_aeroporto(total_aero, novo_aeroporto1); /* adds an airport to the list, I also think that it won't reach here when executing */
        strcpy(total_aero->naero.codigo, novo_voo.aero_partida);
        total_aero->naero.voos_saem++;
        strcpy(total_aero->naero.aero_barato, novo_voo.aero_chegada);
        strcpy(total_aero->naero.aero_tarde, novo_voo.aero_chegada);
        total_aero->naero.preco_barato=novo_voo.preco;
        total_aero->naero.hora_tarde=novo_voo.hora_partida;
        total_aero->naero.minuto_tarde=novo_voo.minuto_partida;
        total_aero->naero.situacao='a';
        total_aero->naero.voos = NULL;
        insere_voo((total_aero->naero).voos, novo_voo);
        }
    else{
        t->naero.voos_saem++;
        if(novo_voo.preco < (t->naero.preco_barato)){
            t->naero.preco_barato=novo_voo.preco;
            strcpy(t->naero.aero_barato, novo_voo.aero_chegada);
        };
        if((t->naero.hora_tarde<novo_voo.hora_partida) || (t->naero.hora_tarde==novo_voo.hora_partida && t->naero.minuto_tarde<novo_voo.minuto_partida)){
            (t->naero.hora_tarde=novo_voo.hora_partida);
            (t->naero.minuto_tarde=novo_voo.minuto_partida);
            strcpy(t->naero.aero_tarde, novo_voo.aero_chegada);
        }
        insere_voo(t->naero.voos, novo_voo);
    }


    for(t=total_aero; t != NULL; t = t->prox){

        if(!strcmp(t->naero.codigo, novo_voo.aero_chegada)){
            t->naero.voos_chegam++;
            return;
        }
    }

提前致谢,如果有什么不清楚,请告诉我,我会尽力解释它,

我将把 .h 文件放在这里:

 typedef struct _st_voo{
char codigo[7];
char aero_partida[4];
char aero_chegada[4];
int hora_partida;
int minuto_partida;
char hora_chegada[6];
float preco;
 }voo;



 struct nodev {
voo nvoo;
struct nodev *prox;
 };
 typedef struct nodev *listav;



 typedef struct _st_aeroporto{
char codigo[4];
int voos_saem;
int voos_chegam;
char aero_barato[4];
char aero_tarde[4];
float preco_barato;
int hora_tarde;
int minuto_tarde;
char situacao;
listav voos;
 }aeroporto;


 struct nodea {
aeroporto naero;
struct nodea *prox;
 };
 typedef struct nodea *listaa;



 void guarda_voo();

 void info_aeroporto();

 void insere_voo(listav x, voo novo_voo);

 void remove_voo(listav x, voo cancelado);

 void insere_aeroporto(listaa x, aeroporto novo_aero);

I've been programming for some time now, not that much, but it seems really difficult for me to find where the segmentation fault is, already tried pretty much everything :(. This is for a school project and teachers don't usually help on this things.

Well, here it is, the code:

#include "funcoes2.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

listaa total_aero = NULL;

void guarda_voo()
{
    listaa t; /* struct list of airports */
    voo novo_voo;
    aeroporto novo_aeroporto1;
    aeroporto novo_aeroporto2;


    scanf("%s %s %s %d:%d %s %f\n", novo_voo.codigo, novo_voo.aero_partida, novo_voo.aero_chegada, &novo_voo.hora_partida, &novo_voo.minuto_partida, novo_voo.hora_chegada, &novo_voo.preco);

    for(t=total_aero; t != NULL; t = t->prox){
        if(!strcmp(t->naero.codigo, novo_voo.aero_partida))
            break;
    }

    if(t == NULL){
        insere_aeroporto(total_aero, novo_aeroporto1); /* adds an airport to the list, I also think that it won't reach here when executing */
        strcpy(total_aero->naero.codigo, novo_voo.aero_partida);
        total_aero->naero.voos_saem++;
        strcpy(total_aero->naero.aero_barato, novo_voo.aero_chegada);
        strcpy(total_aero->naero.aero_tarde, novo_voo.aero_chegada);
        total_aero->naero.preco_barato=novo_voo.preco;
        total_aero->naero.hora_tarde=novo_voo.hora_partida;
        total_aero->naero.minuto_tarde=novo_voo.minuto_partida;
        total_aero->naero.situacao='a';
        total_aero->naero.voos = NULL;
        insere_voo((total_aero->naero).voos, novo_voo);
        }
    else{
        t->naero.voos_saem++;
        if(novo_voo.preco < (t->naero.preco_barato)){
            t->naero.preco_barato=novo_voo.preco;
            strcpy(t->naero.aero_barato, novo_voo.aero_chegada);
        };
        if((t->naero.hora_tarde<novo_voo.hora_partida) || (t->naero.hora_tarde==novo_voo.hora_partida && t->naero.minuto_tarde<novo_voo.minuto_partida)){
            (t->naero.hora_tarde=novo_voo.hora_partida);
            (t->naero.minuto_tarde=novo_voo.minuto_partida);
            strcpy(t->naero.aero_tarde, novo_voo.aero_chegada);
        }
        insere_voo(t->naero.voos, novo_voo);
    }


    for(t=total_aero; t != NULL; t = t->prox){

        if(!strcmp(t->naero.codigo, novo_voo.aero_chegada)){
            t->naero.voos_chegam++;
            return;
        }
    }

Thanks in advance, if something isn't that clear, tell me, I'll try to explain it

I'll put here the .h file:

 typedef struct _st_voo{
char codigo[7];
char aero_partida[4];
char aero_chegada[4];
int hora_partida;
int minuto_partida;
char hora_chegada[6];
float preco;
 }voo;



 struct nodev {
voo nvoo;
struct nodev *prox;
 };
 typedef struct nodev *listav;



 typedef struct _st_aeroporto{
char codigo[4];
int voos_saem;
int voos_chegam;
char aero_barato[4];
char aero_tarde[4];
float preco_barato;
int hora_tarde;
int minuto_tarde;
char situacao;
listav voos;
 }aeroporto;


 struct nodea {
aeroporto naero;
struct nodea *prox;
 };
 typedef struct nodea *listaa;



 void guarda_voo();

 void info_aeroporto();

 void insere_voo(listav x, voo novo_voo);

 void remove_voo(listav x, voo cancelado);

 void insere_aeroporto(listaa x, aeroporto novo_aero);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

紅太極 2024-11-10 19:59:48

你的代码输入全乱了。由于我们无法查看或执行所有代码,因此不太可能有人能够找到它。您有 2 个选择:

  • 正确方法:了解如何使用 gdb 调试代码: http://www.gnu.org/software/gdb/
  • 肮脏的方式:使用 printf() 在代码的不同部分将语句打印到控制台,以帮助您识别位置和位置当发生分段错误时。

Your code entry got all messed up. Since we can't see all the code or execute it, it's not likely someone will be able to find it. You have 2 options:

  • Right way: Learn how to debug your code using gdb: http://www.gnu.org/software/gdb/
  • Dirty way: Use printf() to print statements to the console at different parts of your code to help you identify where and when the segmentation fault occurs.
掩饰不了的爱 2024-11-10 19:59:48

啊,谷歌是你的朋友,但我会继续说:很明显你需要一个调试器。如果您在 Linux 或 UNIX 操作系统上运行它,那么您需要使用 gdb (http://www.gnu.org/software/gdb/),而在 Windows 领域,Visual Studio 及其同胞附带了内置的调试器如果您使用其中之一来查找分段错误,应该不会花费任何时间。

对于 GDB,调用示例可在 http://sourceware .org/gdb/current/onlinedocs/gdb/Sample-Session.html#Sample-Session,一个专门针对分段错误的链接位于此链接:http://www.unknownroad.com/rtfm/gdbtut/gdbsegfault.html

Ah, Google is your friend here, but I'll go ahead and say it: its pretty obvious you need a debugger. If you're running this on Linux or a UNIX OS then you'll need to use gdb (http://www.gnu.org/software/gdb/) and in Windows land Visual Studio and its compatriots come with a debugger built in. It should take no time if you're using one of those to find the segmentation fault.

For GDB, invocation samples are available at http://sourceware.org/gdb/current/onlinedocs/gdb/Sample-Session.html#Sample-Session, and one specifically for segmentation faults is at this link: http://www.unknownroad.com/rtfm/gdbtut/gdbsegfault.html.

碍人泪离人颜 2024-11-10 19:59:48

在尝试使用 strcpy 或 scanf 复制到字符串之前,请检查是否已为字符串分配了空间。

正如其他人提到的,使用调试器。如果您只是在调试器中运行直到它失败,您将能够查看回溯以查看导致失败的调用。

您可以使用 printf 语句来显示值 - “穷人”的调试器,但使用调试器您会更快地发现问题。

Check that you have allocated space for your strings before trying to copy into them with strcpy or scanf.

As others mentioned,use the debugger. If you just run within the debugger until it fails, you will be able to look at the traceback to see the calls leading to the failure.

You can use printf statements to display values - the "poor" man's debugger, but you'll find problems faster with a debugger.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文