Thanks for reply. Here is the code
Thanks for reply. Here the code.
void trie_insert(Trie *trie, char *key, void *value)
{
TrieNode **rover;
TrieNode *node;
char *p;
int c;
static int node_memory;
/* Cannot insert NULL values */
if (value == NULL) {
return;
}
/* Search down the trie until we reach the end of string,
* creating nodes as necessary */
rover = &trie->root_node;
p = key;
while (*p!='\0') {
node = *rover;
if (node == NULL) {
/* Node does not exist, so create it */
node = (TrieNode *) malloc(sizeof(TrieNode));
memset(node, 0,sizeof(TrieNode));
if(node){
++node_memory;
//printf("node malloc memory count :- %d",node_memory);
}
// printf("\n created new node %x", node);
/* Link in to the trie */
*rover = node;
}
/* One more use of this node */
++node->use_count;
/* Current character */
c = *p;
/* Reached the end of string? If so, we're finished. */
if (c == '\0') {
/* Set the data at the node we have reached */
node->data = value;
break;
}
/* Advance to the next node in the chain */
rover = &node->next[c];
++p;
}
node->data = value;
}
here is the gdb o/p i tried to print the node but its not allocating memory giving null
(gdb) f 1
#1 0x08048984 in trie_insert (trie=0x8055028, key=0xbfef9338 "angelnympho.com\n", value=0xbfef9338) at trietest.c:123
123 memset(node, 0,sizeof(TrieNode));
(gdb) list
118 if (node == NULL) {
119
120
121 /* Node does not exist, so create it */
122 node = (TrieNode *) malloc(sizeof(TrieNode));
123 memset(node, 0,sizeof(TrieNode));
124 if(node){
125 ++node_memory;
126 //printf("node malloc memory count :- %d",node_memory);
127 }
(gdb) print node_memory
$1 = 581262
(gdb) print node
$2 = (TrieNode *) 0x0
(gdb)
Last edited by Anuradha12345; 08-10-2012 at 01:23 AM.
|