-3

Am I not allowed to delete a object ? Is that why it segfaults and can I use a self referential class as below or should I use it alternatively ? advanced thanks

I actually trying to build a trie still the starting stage

 #include <iostream>
 #include <string>

 using namespace std;
 #define NUMCHAR 26

typedef struct t_struct{
    bool iscomplete;
    t_struct * val[NUMCHAR];
};



class t_tree
{
private:
    bool iscomplete;
    t_tree * val[NUMCHAR];
public:
    t_tree* init(t_tree* node);
    void uninit(t_tree* node);
    t_tree* add_word(t_tree * node , const char* str);
    bool find_word(t_tree *node , const char* str);
};


t_tree* t_tree :: init(t_tree* node)
{
    cout << "Enter Initilized " << endl;
    if(node == NULL) node = new t_tree;
    node->iscomplete = false;
    for(int i = 0 ; i < NUMCHAR ; i++) node->val[i] = NULL;

    cout << "Node Initilized " << endl;
    return node;
}

void t_tree :: uninit(t_tree* node)
{
    cout << "Here  " <<node << endl;
    if(node == NULL) return;


    for(int i = 0 ; i < NUMCHAR ; i++) uninit(node->val[i]) ;

    cout << "Here deleted  " <<node << endl;
    delete node;
    cout << "Here deleted  " <<node << endl;
    node = NULL;
    cout << "Node Uninitilized " << endl;
}


t_tree* t_tree :: add_word (t_tree* node,const char* str)
{
    if (str[0] == '\0')  node->iscomplete = true;

    else{
    unsigned int ch = str[0] - 'a';
    if (node->val[ch] == NULL) node->val[ch] = init(node->val[ch]);
    str++;
    add_word(node->val[ch],str);
    }

return node;
}

int main()
{
    t_tree root;
    root.init(&root);
    root.uninit(&root);
    return 0;

}
ajax_velu
  • 286
  • 3
  • 16
  • 5
    `How Do I debug this Segfault` with a debugger? – user657267 Jul 30 '14 at 07:30
  • 1
    Before you learn debugging (which you should learn) you need to learn C++ first, and how to handle classes and class instances (objects), because right now you treat objects and member functions more like it were C than C++, which leads to your problems. I suggest you check out [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and find a good tutorial there. – Some programmer dude Jul 30 '14 at 07:33

2 Answers2

3

You're trying to delete t_tree* node in uninit(), but your t_tree* node is actually &root, which is not a heap object, so you cant call delete on it.

Marson Mao
  • 2,935
  • 6
  • 30
  • 45
0

Your fundamental mistake with this is not understanding the difference between the heap and the stack.

Many languages only use a heap for objects like this, and deleting (or similar, such as Dispose()) can be called on them (however, even then, such languages typically use the new keyword to create such objects).

Here, you're trying to call delete, a heap operation, on an object allocated on the stack. This is not allowed at all.

This is such a fundamental concept in C++ its very important to learn it.

gbjbaanb
  • 51,617
  • 12
  • 104
  • 148
  • (I am mainly a C programmer) So what you are saying is new does not operate on malloc i.e allocate space from heap? I see from other sources that new allocates memory from "Free store". So when I use new space is not actually allocated from Heap ...? – ajax_velu Jul 30 '14 at 07:59
  • you didn't use new - you created the root object on the stack `t_tree root;` creates an object of type t_tree. if you had said `t_tree* root = new t_tree;` then you would have had an object on the heap that you could call `delete` on later (as you were doing). – gbjbaanb Jul 30 '14 at 19:13