Hi, i need an help for an error.
When I run this code it crush, with this error:
Unhandled exception at 0x77da331f in ES_7.exe: 0xC0000005: Access violation reading location 0x00000004.
Process returned -1073741819 (0xC0000005)

I translated some parts of the code to better understanding
NB: When I run this code with the debugger, it doesn't crush and works properly

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

typedef char elemento;
typedef struct nodo *tree_pointer;
typedef struct nodo
{
    elemento key;
    tree_pointer sx;
    tree_pointer dx;
} albero;
tree_pointer radice = NULL;

tree_pointer creaalbero(tree_pointer);
void preorder(tree_pointer);
void inorder(tree_pointer);
void postorder(tree_pointer);

int main()
{
    int risp;
    if(radice==NULL)
    {
    radice=malloc(sizeof(tree_pointer));
    printf("Enter the value of the radix\n");
    scanf("%c",&radice->key);
    radice->sx=NULL;
    radice->dx=NULL;
    }
    creaalbero(radice);
    do{
    printf("0.EXIT\n");
    printf("1.PREORDER\n");
    printf("2.INORDER\n");
    printf("3.POSTORDER\n");
    scanf("%d",&risp);

    switch(risp)
        {
        case 1:
        printf("***** PREORDER *****\n");
            preorder(radice);
            printf("\n");
            break;
        case 2:
        printf("***** INORDER *****\n");
            inorder(radice);
            printf("\n");
            break;
        case 3:
        printf("***** POSTORDER *****\n");
            postorder(radice);
            printf("\n");
            break;
}

}while(risp!=0);
}
tree_pointer creaalbero(tree_pointer padre)
{
    char r;
    char val;
    fflush(stdin);
    printf("%c got a sx son? (s/n) \n",padre->key);
    scanf("%c",&r);
    if (r=='s')
    {
        fflush(stdin);
        printf("Enter the value of the sx son of %c\n",padre->key);
        scanf("%c",&val);
        padre->sx=malloc(sizeof(tree_pointer));
        padre->sx->key=val;
        padre->sx->sx=NULL;
        padre->sx->dx=NULL;
        creaalbero(padre->sx);
    }
    else padre->sx=NULL;
    fflush(stdin);
    printf("%c got a dx son? (s/n) \n",padre->key);
    scanf("%c",&r);
    fflush(stdin);
    if(r=='s')
    {
        printf("Enter the value of the dx son of %c\n",padre->key);
        scanf ("%c",&val);
        fflush(stdin);
        padre->dx=malloc(sizeof(tree_pointer));
        padre->dx->key=val;
        padre->dx->sx=NULL;
        padre->dx->dx=NULL;
        creaalbero(padre->dx);
    }
    else padre->dx=NULL;
    return(padre);
}

void inorder(tree_pointer ptr)
{

    if (ptr)
    {
        inorder(ptr->sx);
        printf("%4c", ptr->key);
        inorder(ptr->dx);
    }
}

void postorder(tree_pointer ptr)
{

if (ptr) {
postorder(ptr->sx);
postorder(ptr->dx);
printf("%4c", ptr->key);
}
}

void preorder(tree_pointer ptr)
{

if (ptr) {
printf("%4c", ptr->key);
preorder(ptr->sx);
preorder(ptr->dx);
}
}

Edited 4 Years Ago by deceptikon: Moved to the C subforum

One obvious problem I can see is that you call malloc(sizeof(treepointer)) to allocate memory for a node of the tree, but that will only allocate enough memory to store a pointer.

You need to allocate enough memory to store an entire node, so you need to use sizeof(nodo) as the argument to malloc.

If you mean this tipe of malloc

radice=malloc(sizeof(nodo));

The compiler give me this error:
'nodo' undeclared (first use in this function)

Sorry for my bad english!
Anyway the hint was right, now the program runs how it should.
Thanks

This article has been dead for over six months. Start a new discussion instead.