And I'm back.
Inside my add_word function, I am receiving a SIGSEGV. It occurs on this line:
Code:
oldlen = strlen( ( pc = pnode->translation ) );
I have no idea what I'm doing wrong here. If someone could tell me what stupid thing I am doing, I'd be very grateful.
Code:
/* add word to trie, with translation
input: word and translation
output: non-zero if new node added, zero otherwise
postcondition: word exists in trie
*/
int add_word(const char * word, char * translation) {
unsigned int i , oldlen , newlen;
p_trie_node pnode = proot;
char * pc;
for( i = 0; word[ i ] != '\0'; i++ ){
if( !( pnode->children[ ( unsigned int )( word[ i ] ) ] ) )
pnode->children[ ( unsigned int )( word[ i ] ) ] = new_node();
if( !( pnode = pnode->children[ ( unsigned int )( word[ i ] ) ] ) )
return 0;
}
oldlen = strlen( ( pc = pnode->translation ) );
newlen = oldlen + strlen( translation ) + ( ( oldlen ) ? 2 : 1 );
if( !( pnode->translation = realloc( pc , newlen ) ) )
return 0;
if( oldlen ) {
pnode->translation[ oldlen + 1 ] = ',';
pc = pnode->translation + oldlen + 2;
}
pc = pnode->translation;
strcpy( pc , translation );
return 1;
}
And here is new_node:
Code:
/* allocate new node on the heap
output: pointer to new node (must be freed) */
p_trie_node new_node(void) {
int i;
p_trie_node pnode = malloc( sizeof( p_trie_node ) );
if( pnode ) {
pnode->translation = NULL;
for( i = 0; i < ( UCHAR_MAX + 1 ); i++ )
pnode->children[ i ] = NULL;
}
return pnode;
}