我正在尝试树木和节点,但遇到了麻烦。
我有

typedef struct nodo
{
    int cuenta;
    int fondo;
    struct nodo *sig;
}pNodo;
typedef pNodo *tLista;

typedef struct tree
{
    int RUT;
    struct tree *izq;
    struct tree *der;
    struct nodo *tLista;
}tTree;
typedef tTree *tABB;
typedef tTree *tNodo;

void crearArbol(tABB arbol)
{
    tABB nuevoA;
    nuevoA=(tABB)malloc(sizeof(tTree));
    arbol=nuevoA;
    arbol->izq=NULL;
    arbol->der=NULL;
}

int AgregarCuenta(tABB *arbol,int rut,int id_cuenta,int saldo)
{
    tABB AUX;
    AUX=*arbol;
    tLista nuevaC;
    int i=1;
    if(AUX==NULL)
    {
        crearArbol(AUX);
        if(id_cuenta==1)
        {
            (AUX->tLista)->fondo=saldo;
            return 0;
        }
        else
        {
            return -1;
        }
    }
    else
    {
        if(rut==AUX->RUT)
        {
            while(AUX->tLista!=NULL)
            {
                if(AUX->tLista->cuenta==id_cuenta)
                {
                    return -1;
                }
                else
                {
                    AUX->tLista=AUX->tLista->sig;
                    i++;
                }
            }
            nuevaC=(tLista)malloc(sizeof(pNodo));
            nuevaC->sig=NULL;
            nuevaC->cuenta=i;
            nuevaC->fondo=saldo;
            AUX->tLista=nuevaC;
            return 0;
        }
        else
        {
            if(rut<AUX->RUT)
            {
                AUX=AUX->izq;
                AgregarCuenta(&AUX,rut,id_cuenta,saldo);
            }
            else
            {
                AUX=AUX->der;
                AgregarCuenta(&AUX,rut,id_cuenta,saldo);
            }
        }
    }
}

int main()
{
    tABB arbolillo;
    crearArbol(arbolillo);
    AgregarCuenta(&arbolillo, 18020200, 1, 9999);

    return 0;
}


它死于“(AUX-> tLista)-> fondo = saldo;”在AgregarCuenta函数中。与“ EXC_BAD_ACCESS”

我究竟做错了什么?

最佳答案

我相信crearArbol需要一个指向指针的指针作为输入:

void crearArbol(tABB *arbol)
{
    tABB nuevoA = (tABB)malloc(sizeof(tTree));
    if (nuevoA == 0)
        ...handle out of memory error...
    *arbol = nuevoA;
    (*arbol)->izq = NULL;
    (*arbol)->der = NULL;
}


并致电:

crearArbol(&AUX);


否则,您不会将值“返回”给调用代码。



此外,在此之后,您将:

    if(id_cuenta==1)
    {
        (AUX->tLista)->fondo=saldo;
        return 0;
    }


但是,如果您刚刚调用crearArbol(),则不会初始化tLista成员,因此它指向垃圾。您需要分配指向它的空间,并将tLista成员设置为指向它。那么您可以设置该分配空间的fondo成员。确保初始化每个结构的每个成员...



更新1:crearArbol()中的固定表示法-需要额外的括号。您还必须担心在crearArbol()中对main()的调用,这是我之前错过的。您缺少return中的AgregarCuenta(),或者返回类型应该为void,该函数中的其他return语句将丢失该值。由于您不检查返回的值,因此不清楚哪个更好。



更新2:此代码编译并运行。当然,它会泄漏内存。

#include <stdlib.h>

typedef struct nodo
{
    int cuenta;
    int fondo;
    struct nodo *sig;
}pNodo;
typedef pNodo *tLista;

typedef struct tree
{
    int RUT;
    struct tree *izq;
    struct tree *der;
    struct nodo *tLista;
}tTree;
typedef tTree *tABB;
typedef tTree *tNodo;

static void crearArbol(tABB *arbol)
{
    tABB nuevoA = (tABB)malloc(sizeof(tTree));
    *arbol=nuevoA;
    (*arbol)->izq = NULL;
    (*arbol)->der = NULL;
    (*arbol)->tLista = malloc(sizeof(pNodo));
    (*arbol)->tLista->cuenta = -1;
    (*arbol)->tLista->fondo = -1;
    (*arbol)->tLista->sig = NULL;
}

static int AgregarCuenta(tABB *arbol, int rut, int id_cuenta, int saldo)
{
    tABB AUX;
    AUX=*arbol;
    tLista nuevaC;
    int i=1;
    if(AUX==NULL)
    {
        crearArbol(&AUX);
        if(id_cuenta==1)
        {
            (AUX->tLista)->fondo=saldo;
            return 0;
        }
        else
        {
            return -1;
        }
    }
    else
    {
        if(rut==AUX->RUT)
        {
            while(AUX->tLista!=NULL)
            {
                if(AUX->tLista->cuenta==id_cuenta)
                {
                    return -1;
                }
                else
                {
                    AUX->tLista=AUX->tLista->sig;
                    i++;
                }
            }
            nuevaC=(tLista)malloc(sizeof(pNodo));
            nuevaC->sig=NULL;
            nuevaC->cuenta=i;
            nuevaC->fondo=saldo;
            AUX->tLista=nuevaC;
            return 0;
        }
        else
        {
            if(rut<AUX->RUT)
            {
                AUX=AUX->izq;
                return AgregarCuenta(&AUX,rut,id_cuenta,saldo);
            }
            else
            {
                AUX=AUX->der;
                return AgregarCuenta(&AUX,rut,id_cuenta,saldo);
            }
        }
    }
}

int main(void)
{
    tABB arbolillo;
    crearArbol(&arbolillo);
    AgregarCuenta(&arbolillo, 18020200, 1, 9999);

    return 0;
}


当使用valgrind运行时,我得到:

==25013== Memcheck, a memory error detector
==25013== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==25013== Using Valgrind-3.6.0 and LibVEX; rerun with -h for copyright info
==25013== Command: tree
==25013==
==25013== Conditional jump or move depends on uninitialised value(s)
==25013==    at 0x100000DC0: AgregarCuenta (tree.c:54)
==25013==    by 0x100000EC5: main (tree.c:95)
==25013==
==25013== Conditional jump or move depends on uninitialised value(s)
==25013==    at 0x100000E52: AgregarCuenta (tree.c:77)
==25013==    by 0x100000EC5: main (tree.c:95)
==25013==
==25013==
==25013== HEAP SUMMARY:
==25013==     in use at exit: 184 bytes in 5 blocks
==25013==   total heap usage: 5 allocs, 0 frees, 184 bytes allocated
==25013==
==25013== LEAK SUMMARY:
==25013==    definitely lost: 64 bytes in 2 blocks
==25013==    indirectly lost: 32 bytes in 2 blocks
==25013==      possibly lost: 0 bytes in 0 blocks
==25013==    still reachable: 88 bytes in 1 blocks
==25013==         suppressed: 0 bytes in 0 blocks
==25013== Rerun with --leak-check=full to see details of leaked memory
==25013==
==25013== For counts of detected and suppressed errors, rerun with: -v
==25013== Use --track-origins=yes to see where uninitialised values come from
==25013== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)

关于c - 合并树和节点,无法访问值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6011798/

10-12 20:46