我的程序有一个分段错误

时间:2015-06-17 23:49:36

标签: c

我的程序出现了分段错误,我不知道它在哪里。 这是相关的代码。程序使用树的inorder和postorder遍历从列表生成树。

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

#define FALSE 0
#define TRUE 1

typedef struct btreenode{
    struct btreenode *rightchild;
    struct btreenode *leftchild; 
    float val;
    int data;
}Node;


struct btreenode* Treebuilding(float *, int *, int , int, float *, int *, int, int);
struct btreenode* cargar_arbol(char *);
struct btreenode* new_node(int, float);

int main(){
    FILE *fp, *fap;
    char cmd[257], par[257];
    int num; float val;
    struct btreenode *root;
    root = NULL;
    if ((fp = fopen("rutina.dat", "r")) == 0){
        printf("Error al cargar el archivo rutina.dat\n");
        return 1;
    }
    fap =fopen("output.dat", "w");
    while(fscanf(fp, "%[^ \n]", cmd)!= 0){
        if(strcmp("Cargar", cmd)==0){
            fscanf(fp, "%s", par);
            root = cargar_arbol(par);
            if(root != NULL)    
                fprintf(fap, "Datos Cargados\n");
            else
                fprintf(fap, "No se pudo cargar datos\n"); 
        }
    }
    return 0;
}

struct btreenode* Treebuilding(float InOrder[],int inorderName[], int instart, int inend,float PostOrder[], int postorderName[], int poststart,int  postend){
    int i;
    if (instart > inend|| poststart > postend){
        return NULL;
    }
    int rootname = postorderName[postend];  
    struct btreenode* root = new_node(postorderName[postend],PostOrder[postend]); 
    int k =0;

    for (i=0; i < inend; i++){
        if(inorderName[i] == rootname){
            k=i;
            break;
        }
    }

    root->leftchild = Treebuilding (InOrder, inorderName, instart, k-1, PostOrder,postorderName, poststart, poststart+k-(instart+1));
    root->rightchild= Treebuilding (InOrder,inorderName, k+1, inend, PostOrder,postorderName, poststart+k-instart, postend);

    return root;
}

struct btreenode* cargar_arbol(char *nombre){
    FILE* fp; int size, *inorderName, *postorderName;
    float *inorder, *postorder; int i;
    if ((fp = fopen(nombre, "r")) == 0){
        printf("Error al cargar %c", nombre);
        return NULL;
    }
    fscanf(fp, "%d", &size);
    inorder = (float*)malloc(sizeof(float)*size);
    inorderName = (int*)malloc(sizeof(int)*size);
    postorder = (float*)malloc(sizeof(float)*size);
    postorderName = (int*)malloc(sizeof(int)*size);

    int instart = 0;
    int inend = size-1;
    int poststart = 0;
    int postend = size -1;

    for(i=0; i<size; i++){
        fscanf(fp, "Liceo %d | %f", &inorderName[i],  &inorder[i]);
    }
    for(i=0; i<size ; i++){
        fscanf(fp, "Liceo %d | %f", &postorderName[i],  &postorder[i]);
    }
    return Treebuilding(inorder, inorderName, instart, inend, postorder, postorderName, poststart, postend);
}

gdb说它在函数treebuilding()中,但我看不到确切的位置。

1 个答案:

答案 0 :(得分:1)

在Linux上,您可以通过ulimit -c unlimited命令启用核心转储。使用调试符号(gdb -g -O0)编译程序,然后执行程序并等待崩溃。

使用核心转储,您可以使用gdb -c core <program>分析崩溃。 gdb命令bt显示崩溃的回溯,并显示错误的源文件和行。