【问题标题】:Program doesn't run well (means outputs (printf)) but in gdb t.exe it works well程序运行不佳(意味着输出(printf))但在 gdb t.exe 中运行良好
【发布时间】:2019-06-20 00:55:06
【问题描述】:

我目前遇到了一个奇怪的显示问题。 当我这样做运行程序时:t我有这个结果: result of the execution of the t.exe

这基本上是我程序的开始:

但是,当我像这样使用 gdb 时:gdb t.exe,一切正常:result of the execution in debug mode

下面是我的代码(对不起,它不干净:/)

#include <stdio.h>
#include <stdlib.h>


typedef int typeElem;

typedef struct  aa {
  typeElem v ;
  struct aa * fg , * fd ;  // les fils gauche et droit
} abr;

#define arbre_vide NULL

typedef abr *arbre;

typedef int boolean;

arbre consa(typeElem x, arbre fils_gauche, arbre fils_droit) {
    arbre N = malloc ( sizeof( arbre ) ) ;
    N->fg = fils_gauche;
    N->fd = fils_droit;
    N->v = x;
    return N;
}

void destruct(arbre ABR) {
    free(ABR);
}

arbre gauche(arbre ABR) {
    return ABR->fg;
}

arbre droit(arbre ABR) {
    return ABR->fd;
}

int estVideArbre(arbre ABR) {
    return (ABR == NULL);
}

typeElem racine(arbre ABR){
    return (ABR->v);
}

/*
void destructAbr(arbre ABR){
    if(!estVideArbre(ABR)){
        destructAbr(gauche(ABR));
        destructAbr(droit(ABR));        
    }
}
*/

void infixe(arbre ABR){
    if(!(ABR == NULL)){
        infixe(ABR->fg);
        printf("%d ", ABR->v);
        infixe(ABR->fd);
    }
    else {
        //printf("est null \n");
    }
}

void prefixe(arbre ABR){
    if(!estVideArbre(ABR)){
        printf("%d ", ABR->v);
        infixe(ABR->fg);
        infixe(ABR->fd);
    }
}

void postfixe(arbre ABR){
    if(!estVideArbre(ABR)){
        infixe(gauche(ABR));
        infixe(droit(ABR));
        printf("%d ", racine(ABR));
    }
}
int nbrTotalNoeuds(arbre ABR) {
    if(estVideArbre(ABR))
        return 0;
    return 1 + nbrTotalNoeuds(gauche(ABR)) + nbrTotalNoeuds(droit(ABR));
}


int nbreNoeudsDe2Fils(arbre ABR) {
    if( !estVideArbre(gauche(ABR)) && !estVideArbre(droit(ABR)) ){
        return 1 + nbreNoeudsDe2Fils(gauche(ABR)) + nbreNoeudsDe2Fils(droit(ABR));
    }
    else if (!estVideArbre(gauche(ABR)) && estVideArbre(droit(ABR)) ) {
        return nbreNoeudsDe2Fils(gauche(ABR));
    }
    else if (estVideArbre(gauche(ABR)) && !estVideArbre(droit(ABR)) ) {
        return nbreNoeudsDe2Fils(droit(ABR));
    }
    else {
        return 0;
    }
}


int nbreNoeudsDe1Fils(arbre ABR) {
    if( gauche(ABR) != NULL && droit(ABR) != NULL ) {
        return 0 + nbreNoeudsDe1Fils(gauche(ABR)) + nbreNoeudsDe1Fils(droit(ABR));
    }
    else if( gauche(ABR) != NULL && droit(ABR) == NULL ) {
        return 1 + nbreNoeudsDe1Fils(gauche(ABR));
    }
    else if( gauche(ABR) == NULL && droit(ABR) != NULL ) {
        return 1 + nbreNoeudsDe1Fils(droit(ABR));
    }
    else {
        return 0;
    }
}


/*
        5
       / \
      3   6
     / \   \
    2   4   7
             \
              9
*/


int main() { //test


    printf("Lancement du programme \n");
    arbre rac_g = consa(2,arbre_vide,arbre_vide);
    arbre rac_d = consa(4,arbre_vide,arbre_vide);

    arbre rac_d_d_d = consa(9,arbre_vide,arbre_vide);
    arbre rac_d_d = consa(7,arbre_vide,rac_d_d_d);

    arbre fg = consa(3,rac_g,rac_d);
    arbre fd = consa(6,arbre_vide,rac_d_d);

    arbre sommet = consa(5,fg,fd);

    printf("\nEn ordre infixe : ");
    infixe(sommet);

    printf("\nEn ordre prefixe : ");
    prefixe(sommet);
    printf("\nEn ordre postfixe : ");
    postfixe(sommet);


    printf("\nNombre de noeuds dans cette arbre : %d\n", nbrTotalNoeuds(sommet));
    printf("Nombre de noeuds a 2 fils : %d\n", nbreNoeudsDe2Fils(sommet));
    printf("Nombre de noeuds a 1 fils : %d\n", nbreNoeudsDe1Fils(sommet));

    // destructAbr(sommet);

    return 0;
}

【问题讨论】:

  • 这是一个完美的例子,为什么人们应该从不 typedef 指针(可能除了函数指针)

标签: c gcc gdb printf mingw


【解决方案1】:

这个

  arbre N = malloc ( sizeof( arbre ) ) ;

不符合您的预期。它只为指针分配内存,而不是为指针指向的内容分配内存。

  arbre N = malloc(sizeof *N);

改为。


我不明白您为什么为或多或少相同的事物定义三个名称。我的建议是放弃abrarbre,只使用struct aastruct aa *

更少的名字,更少的混乱,更少的错误。

【讨论】:

  • 这是一个完美的例子,为什么人们应该从不 typedef 指针(可能除了函数指针)
  • @P__J__:同意。不过,从技术上讲,这样做不是问题。
  • 但它使程序难以阅读和理解。它还使程序容易出现愚蠢的错误 - 这是愚蠢错误的经典示例
  • 非常感谢!我会尝试编写更简洁的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-16
  • 2013-07-27
  • 1970-01-01
  • 1970-01-01
  • 2020-03-05
  • 1970-01-01
相关资源
最近更新 更多