1

This is simple ternary tree structure . I have written code correctly but while running it says after some time:

Sorry ternary.exe has stopped working.

Can you tell me the cause of this error.

#include<iostream>
#include<string>
using namespace std;
struct tnode{
    int data[2];
    tnode *ptr[3];
};

void swap(int *a,int *b){
    int t;
    t=*a;
    *a=*b;
    *b=t;
}
//for initializing tnode variables as null or null character 
void newtree(tnode *&T){
    T->data[0]='\0';
    T->data[1]='\0';
    T->ptr[0]=NULL;
    T->ptr[1]=NULL;
    T->ptr[2]=NULL;
}

void fillto(tnode *&T,int a){

    if(T->data[0]=='\0'){
    T->data[0]=a;   
    }
    else if(T->data[0]!='\0'&&T->data[1]=='\0'){
        T->data[1]=a;
        if(T->data[0]>T->data[1])
        swap(T->data[0],T->data[1]);
    }
    else{
        if(a<T->data[0]){

            if(T->ptr[0]==NULL){
            T->ptr[0]=new(tnode);
            newtree(T->ptr[0]); 
            }

            fillto(T->ptr[0],a);
        }
        else if(a>T->data[1]){
            if(T->ptr[2]==NULL){
            T->ptr[2]=new(tnode);
            newtree(T->ptr[2]); 
            }
            fillto(T->ptr[2],a);
        }
        else{

            if(T->ptr[1]==NULL){
                newtree(T->ptr[1]);
                T->ptr[1]=new(tnode);           
            }
            fillto(T->ptr[1],a);    
        }
    }
}

tnode *datatnode(string s){
    int l=0;
    tnode *T;
    tnode *E;
    T=new(tnode);
    char c[0];
    newtree(T);
    E=T;

    while(l<=s.length()){
        c[0]=s[l];
        cout<<atoi(c)<<endl;
        fillto(T,atoi(c));
        l++;
    }
    return E;

}


int main(){
    string s="5398124";
    tnode *T;
    T=new(tnode);
    T=datatnode(s);
    cout<<T->data[0];
    return 0;
}
Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62
dann_
  • 59
  • 1
  • 10
  • `T=new(tnode); T=datatnode(s);` You `new` up some memory, then immediately leak it. Not the cause of the crash though. – BoBTFish Sep 03 '13 at 08:11
  • Have you tried debugging your code? Furthermore: why does your C++ code look like C (aside from substituting `malloc` with `new` and `printf` with `cout`? Actually use the language if your ware writing C++ code. As a side note: You do realize that you are leaking memory, right? – Grizzly Sep 03 '13 at 08:11
  • 2
    Btw: Probably not the only problem with that source, but `char c[0];` looks kind of suspicious to me. – Grizzly Sep 03 '13 at 08:16

3 Answers3

1

You should remove '=' sign as below

 tnode *datatnode(string s){
    int l=0;
    tnode *T;
    tnode *E;
    T=new(tnode);
    char c;
    newtree(T);
    E=T;
    int a = s.length();
    while(l<a){
        c=s[l];
        cout<<atoi(&c)<<endl;
        fillto(T,atoi(&c));
        l++;
    }
    return E;

}
gokhans
  • 210
  • 2
  • 12
0

Its difficult to say from your code (as mentally you have to run it in your head). Better to debug it out. Call some debug at key points in your code and try to locate the exact line of code.... this could produce a lot of debug depending how big your data-set is.

At a guess I would say that you probably hit a bad address or somthing like this, that is usually why programs die un-expectedly and immediatly! So I would suggest being very secure on your pointer checking. For example:

void fillto(tnode *&T,int a){

    if (T != NULL){

        if(T->data[0]=='\0')
        {
            T->data[0]=a;   
        }

        :
        :

    }
    else
    {
        printf("Warning: NULL pointer!\n");
    }

}

Basically any time you use a pointer that is passed in to a function you should check it is not null. This is generally good code practise and may help you to find your bugs :)

Also int initialisation can just be:

int i = 0;

instead of

int i = '\0';
code_fodder
  • 15,263
  • 17
  • 90
  • 167
0

The fundamental flaw that causes the error is in the 'void fillto(tnode *&T,int a)' function:

...
        if(T->ptr[1]==NULL){
            newtree(T->ptr[1]);
            ...
        }

As the function newtree does not check if the pointer is null, you end up dereferencing a NULL pointer in newtree

Come Raczy
  • 1,590
  • 17
  • 26