Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4616660
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:00:49+00:00 2026-05-22T02:00:49+00:00

i’m experimenting with trees and nodes and I having trouble. I have typedef struct

  • 0

i’m experimenting with trees and nodes and I having trouble.
I have

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;
}

It dies on “(AUX->tLista)->fondo=saldo;” at the AgregarCuenta function. with “EXC_BAD_ACCESS”

What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T02:00:50+00:00Added an answer on May 22, 2026 at 2:00 am

    I believe crearArbol requires a pointer to a pointer as input:

    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;
    }
    

    And the call:

    crearArbol(&AUX);
    

    Otherwise, you don’t ‘return’ the value to the calling code.


    Additionally, just after that you have:

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

    But if you’ve just called crearArbol() there is no initialization of the tLista member, so it points at garbage. You need to allocate the space for it to point to, and set the tLista member to point to it; then you can set the fondo member of that allocated space. Make sure you initialize every member of every structure…


    Update 1: fixed notation in crearArbol() – the extra parentheses are needed. You also have to worry about the call to crearArbol() in main(), which I missed before. You are missing a return from AgregarCuenta(), or the return type should be void and the other return statements in the function would lose the value. Since you don’t check the returned value, it is not clear which is better.


    Update 2: this code compiles and runs. It leaks memory, of course.

    #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;
    }
    

    When run with valgrind, I get:

    ==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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.