Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I believe <code>crearArbol</code> requires a pointer to a pointer as input:</p> <pre><code>void crearArbol(tABB *arbol) { tABB nuevoA = (tABB)malloc(sizeof(tTree)); if (nuevoA == 0) ...handle out of memory error... *arbol = nuevoA; (*arbol)-&gt;izq = NULL; (*arbol)-&gt;der = NULL; } </code></pre> <p>And the call:</p> <pre><code>crearArbol(&amp;AUX); </code></pre> <p>Otherwise, you don't 'return' the value to the calling code.</p> <hr> <p>Additionally, just after that you have:</p> <pre><code> if(id_cuenta==1) { (AUX-&gt;tLista)-&gt;fondo=saldo; return 0; } </code></pre> <p>But if you've just called <code>crearArbol()</code> there is no initialization of the <code>tLista</code> member, so it points at garbage. You need to allocate the space for it to point to, and set the <code>tLista</code> member to point to it; then you can set the <code>fondo</code> member of that allocated space. Make sure you initialize every member of every structure...</p> <hr> <p><em>Update 1</em>: fixed notation in <code>crearArbol()</code> - the extra parentheses are needed. You also have to worry about the call to <code>crearArbol()</code> in <code>main()</code>, which I missed before. You are missing a <code>return</code> from <code>AgregarCuenta()</code>, or the return type should be <code>void</code> and the other <code>return</code> statements in the function would lose the value. Since you don't check the returned value, it is not clear which is better.</p> <hr> <p><em>Update 2</em>: this code compiles and runs. It leaks memory, of course.</p> <pre><code>#include &lt;stdlib.h&gt; 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)-&gt;izq = NULL; (*arbol)-&gt;der = NULL; (*arbol)-&gt;tLista = malloc(sizeof(pNodo)); (*arbol)-&gt;tLista-&gt;cuenta = -1; (*arbol)-&gt;tLista-&gt;fondo = -1; (*arbol)-&gt;tLista-&gt;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(&amp;AUX); if(id_cuenta==1) { (AUX-&gt;tLista)-&gt;fondo=saldo; return 0; } else { return -1; } } else { if(rut==AUX-&gt;RUT) { while(AUX-&gt;tLista!=NULL) { if(AUX-&gt;tLista-&gt;cuenta==id_cuenta) { return -1; } else { AUX-&gt;tLista=AUX-&gt;tLista-&gt;sig; i++; } } nuevaC=(tLista)malloc(sizeof(pNodo)); nuevaC-&gt;sig=NULL; nuevaC-&gt;cuenta=i; nuevaC-&gt;fondo=saldo; AUX-&gt;tLista=nuevaC; return 0; } else { if(rut&lt;AUX-&gt;RUT) { AUX=AUX-&gt;izq; return AgregarCuenta(&amp;AUX,rut,id_cuenta,saldo); } else { AUX=AUX-&gt;der; return AgregarCuenta(&amp;AUX,rut,id_cuenta,saldo); } } } } int main(void) { tABB arbolillo; crearArbol(&amp;arbolillo); AgregarCuenta(&amp;arbolillo, 18020200, 1, 9999); return 0; } </code></pre> <p>When run with <code>valgrind</code>, I get:</p> <pre><code>==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) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload