Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7929393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T20:03:45+00:00 2026-06-03T20:03:45+00:00

I’m writing a Trie Tree in C, with these structs: typedef struct sNode Node;

  • 0

I’m writing a Trie Tree in C, with these structs:

typedef struct sNode Node;
struct sNode {
    int word;
    unsigned int nsons;
    Node *next[26];
};

typedef struct {
     Node *root;
} Trie;

But I’m having a problem in this insert function:

int insert(Trie* trie, char word[]){
    int i=0, posiletter, contletters=0;
    char letter=word[0];
    Node *newroot;
    Node *newNode=NULL;
    Node **copyroot;
    Node *current=NULL;
    Node *Nodefather=NULL;
    Node verif;


    if (trie->root=NULL){
        newroot=malloc(sizeof(Node));   
        for (i=0; i<=25; i++) newroot->next[i]=NULL;
        newroot->nsons=0;
        newroot->word=0;
        trie->root=newroot;
    }
    copyroot=&trie->root;
    current=*copyroot;
    while (word[contletters]!='\0'){
        posiletter=(word[contletters])-'a';

        while( (current->next[posiletter])!= NULL ){ // Error in this line
            Nodefather=current;
            current=current->next[posiletter];
        }
        if ( (current->next[letter-'a']) == NULL ){
            newNode=malloc(sizeof(Node));
            //newNode=malloc(sizeof(Node));   
            for (i=0; i<=25; i++) newNode->next[i]=NULL;
            newNode->nsons=0;
            newNode->word=0;
            Nodefather->next[letter-'a']=newNode;
            Nodefather->nsons=(Nodefather->nsons)+1;

        }
        contletters++;

    }
    if  (letter=='\0') Nodefather->next[letter-'a']->word=1; //end of word
    return 1;
}

The function receives a pointer to the tree and an string. If the root of the Trie is NULL, it creates a node, after this, it searchs on the array next a free (NULL) position to store.

The problem is in this line:

while( (current->next[posiletter])!= NULL ){ // Error in this line

It gives me Segmentation fault error when I execute this, and this error on GDB:

Program received signal SIGSEGV, Segmentation fault.
0x0804883f in insert (trie=0x804b008, word=0xbffff230 "abcdef") at trie.c:97
97          while( (current->next[posiletter])!= NULL ){ // Error in this line

And using Valgrind this way: valgrind –tool=memcheck –leak-check=full –track-origins=yes ./trie It give me these errors

==3409== Invalid read of size 4
==3409==    at 0x804883F: insert (trie.c:97)
==3409==    by 0x80485FD: main (triet.c:36)
==3409==  Address 0x8 is not stack'd, malloc'd or (recently) free'd
==3409== 
==3409== 
==3409== Process terminating with default action of signal 11 (SIGSEGV)
==3409==  Access not within mapped region at address 0x8
==3409==    at 0x804883F: insert (trie.c:97)
==3409==    by 0x80485FD: main (triet.c:36)
==3409==  If you believe this happened as a result of a stack
==3409==  overflow in your program's main thread (unlikely but
==3409==  possible), you can try to increase the size of the
==3409==  main thread stack using the --main-stacksize= flag.
==3409==  The main thread stack size used in this run was 8388608.
==3409== HEAP SUMMARY:
==3409==     in use at exit: 4 bytes in 1 blocks
==3409==   total heap usage: 1 allocs, 0 frees, 4 bytes allocated
==3409== 
==3409== LEAK SUMMARY:
==3409==    definitely lost: 0 bytes in 0 blocks
==3409==    indirectly lost: 0 bytes in 0 blocks
==3409==      possibly lost: 0 bytes in 0 blocks
==3409==    still reachable: 4 bytes in 1 blocks
==3409==         suppressed: 0 bytes in 0 blocks
==3409== Reachable blocks (those to which a pointer was found) are not shown.
==3409== To see them, rerun with: --leak-check=full --show-reachable=yes
==3409== 
==3409== For counts of detected and suppressed errors, rerun with: -v
==3409== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 11 from 6)
Segmentation fault

I really appreciate any help. Thanks!

====Edit====
Values in the time of the crash:

(gdb) print current
$1 = (Node *) 0x0
(gdb) print current->next
Isn't possible acess the memory at the address 0x8
(gdb) print current->next[posiletter]
Isn't possible acess the memory at the address 0x8
(gdb) print posiletter
$2 = 0

===Edit2===
Other values requesteds:

(gdb) print contletters
$3 = 0
(gdb) print trie->root
$4 = (Node *) 0x0
(gdb) print trie->root->next[word[0]-'a']
Isn't possible acess the memory at the address 0x8
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T20:03:46+00:00Added an answer on June 3, 2026 at 8:03 pm

    Ah how did I miss that!

    Here is your problem:

    if (trie->root=NULL)
    

    should be

    if (trie->root == NULL)
    

    Note the ==.

    The way you have written it, you set root to NULL which evaluates to false and the root initialization is never done. You could figure it out by the fact that your gdb outputs 0x0 for trie->root

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I am writing an app with both english and french support. The app requests
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.