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 6765937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:46:16+00:00 2026-05-26T14:46:16+00:00

ok so i define my structure like this. struct trie { struct trie *child[26];

  • 0

ok so i define my structure like this.

    struct trie {
        struct trie *child[26];
        int count;
        char letter;
    };

the problem is when i try to fill my trie with words i get a segmentation fault.
ive been told that the problem is that the child variable isn’t pointing to anything and setting them to NULL would fix this. Also creating a second structure would be a good way to achieve this. i am new to C programming and am confused on how to create a second structure to achieve this. any help would be much appreciated.

int addWordOccurrence(const char* word)
{

    struct trie *root;
    root = (struct trie *)malloc(sizeof(struct trie*));
    struct trie *initRoot=root;
    int count;

    int x=strlen(word);
    printf("%d",x);
    int i;
    for(i=0; i<x; i++)
    {  
        int z=word[i]-97;
        if(word[i]=='\n')
        {
            z=word[i-1]-97;
            root->child[z]->count++;
            root=initRoot;
        }

        root->child[z] = (struct trie *)malloc(sizeof(struct trie));
        root->child[z]->letter=word[i];
        root->child[z]=root;
    }
    return 0;
}
  • 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-05-26T14:46:16+00:00Added an answer on May 26, 2026 at 2:46 pm
    root->child[z] = (struct trie *)malloc(sizeof(struct trie));
    root->child[z]->letter=word[i];
    root->child[z]=root;
    

    This is problematic.
    1) What if child[z] was already set?
    2) You never set child[z]->child or child[z]->count to anything

    #2 is causing your segfaults, #1 is a memory leak.

    My solution would be to write a function for allocating new children:

    struct trie* newtrie(char newchar) {
        struct trie* r = malloc(sizeof(struct trie));
        memset(r, 0, sizeof(struct trie));
        r->letter = newchar;
        return r;
    }
    

    Then your code would become:

        if (root->child[z] == NULL)
            root->child[z] = newtrie(word[i]);
        root->child[z]=root;
    

    You also have to change the malloc of root:

    struct trie *root = newtrie(0);
    

    Which is more clear, and avoids the errors I mentioned. http://codepad.org/J6oFQJMb No segfaults after 6 or so calls.

    I’ve also noticed that your code mallocs a new root, but never returns it, so nobody except this function can ever see it. This is also a memory leak.

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

Sidebar

Related Questions

Let's suppose I have a struct like this: struct my_struct { int a; int
Consider this piece of code: struct Trade { float Price; char* time; int shares;
I've defined an opaque structure and related APIs like this: typedef struct foo foo;
I have this complicated structure thingie: #include <stdlib.h> typedef struct { int x; int
suppose a struct defined like this: struct S{ char a[3]; char b[3]; char c[3];
I have a simple structure that is defined like so: typedef struct { int
Hey guys, here is my code. int main() { char buffer[BUFSIZE]; // define our
Here is my problem: in a header I define a structure template type_to_string ,
I have the following struct in C++: #define MAXCHARS 15 typedef struct { char
I have the following structure in c++: #define CGAPI_Exports_Attr __declspec(dllimport) extern CCGAPI_Exports_Attr struct PointCg

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.