krithigal 0 Newbie Poster

Hi
I am using a recursive map. The class and data structures are given below.
While trying to insert a node in hash map I am getting unhandled exception.

typedef struct Item
{
    char *text;
    char *Id;
}ITEM;

class Node;

typedef class Node NODE;

class Node
{
public:

             hash_map<const char*,NODE *> nodeMap;
    ITEM *item;
};

class Tree 
{
public:
    Node *nodePtr;
};

insert()
{
     Node N1;
    N1.item->text = "t1";
  N1.item->Id = "A1";
  N1.nodeMap.insert(pair<const char *, NODE *> ("A1", tnode));
  months.insert(pair <const char*, char *> ("january","feb")); 
 p1 = N1.nodeMap.find("A1");
 insnode = p1->second;


    Node *N2;
   N2 = (NODE *) malloc(sizeof(NODE));
   Item it1;
  it1.text = (char *) malloc(sizeof(char) *10);
  strcpy(it1.text, "ABC");
  N2->item = &it1;
 N2->nodeMap.insert(pair<const char *, NODE *> ("A1", tnode));
 p1 = N1.nodeMap.find("A1");
insnode = p1->second;


}

main()
{
   insert();
}

In the above code block if I try to insert into the hash_map of N1 it is working. But in case of a pointer N2 , the insertion fails with unhandled exception.

Can you pl. let me know why does this occur and how to resolve the same.

Your help will be highly appreciated

regards
krithiga

Dave Sinkula commented: Use code tags. +0