2011-05-15 17 views
0

我正在試驗樹和節點,並遇到了麻煩。 我有組合樹和節點,值無法訪問

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) - >豐多= saldo;」在AgregarCuenta功能。與「EXC_BAD_ACCESS」

我在做什麼錯?

+0

這肯定會幫助,如果使用的代碼英文標識 – sehe 2011-05-15 23:18:44

+1

@sehe:學習抽象... – 2011-05-15 23:20:46

+0

@Jonathan閱讀代碼:我沒有說它不能做:)我想我是說:它現在阻止我讀取你的代碼 – sehe 2011-05-15 23:28:00

回答

0

我相信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()固定的符號 - 需要額外的括號。您還必須擔心在main()中致電crearArbol(),這是我之前錯過的。您從AgregarCuenta()錯過return,或者返回類型應爲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) 
+0

即使有這些變化,錯誤仍然存​​在:s – 2011-05-15 23:08:40

+0

@Diego:你將不得不顯示更新的內存分配代碼讓任何人都能評論你爲什麼仍然有問題。保留問題中的原始代碼,但也顯示更改內容。 – 2011-05-15 23:10:52

+0

現在感謝它的魅力 – 2011-05-15 23:43:13