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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:03:59+00:00 2026-06-01T10:03:59+00:00

I’m trying to implement a program similar to 20 Questions, in which a text

  • 0

I’m trying to implement a program similar to 20 Questions, in which a text file of the questions and guesses for answers are loaded, copied into a char array (where the new space lines are replaced by ‘/0’ in order to split the questions into individual strings). The array works fine after the text file is copied into it. A tree structure is set up to organize the phrases into the yes/no question tree, where the left child is the yes response, while the right is the no response, and leaves are the guesses that the program uses to guess at the end.

The issue that I’m having is that after I build the tree (call treeBuilder from InitTree), the contents of the array where the phrases from the text file were copied became corrupted.

Before I call InitTree, the array contents look like this:

Is it furry? Does it meow? a cat a dog Does it have tusks? Does it have big ears? an elephant a rhinoceros an alligator

After calling it, it looks like this:

Is it furry? -???` ?p ?a dog Does it have tusks? Does it have big ears? an elephant a rhinoceros an alligator

I’ve been testing where it stops working, and within treeBuilder, all of the elements of the array are intact, but as soon as the function call to treeBuilder ends, the array becomes corrupted. I’ve tried protecting the memory by using calloc whenever I allocate memory, and even by making the character array static, which worked in a similar situation where this happened. But all of my preventative measures don’t seem to be working and I’m not sure where the problem lies. I’ve already tried looking at similar cases here on stackoverflow but I couldn’t anything that related to my issue.

This eventually leads to a seg fault, when the program actually starts to use the tree, for obvious reasons.

I’ve tried running gdb, but for whatever reason it won’t let me go through line by line, because it cannot find the line information, and just skips everything until it either prompts for input, or gets a memory error or something, so running gdb isn’t very helpful here. I’m guessing this might be because the main function is in an included file or something. But that’s beside the point.

Here’s the code related to the problem:

struct treeStruct {
    char *string;
    struct treeStruct *left, *right;
};

typedef struct treeStruct *TreeType;

// Builds a tree
void treeBuilder(TreeType tree, char **phrase, long level){
    // Gets the level (number of tabs) of the next phrase
    long nextLevel = countTabs(*phrase + strlen(*phrase) + 1);

    tree->string = *phrase + level; // Assigns the response pointer to the tree array

    // Move the pointer to the next string, since the the strings need to be
    // put into the tree in linear order
    (*phrase) += strlen(*phrase) + 1;

    if (level >= nextLevel){
    // Compares the current level with the level of the next string
            // to determine if returning up the tree is necessary;
            // This should be the answer to a question.
            tree->left = NULL;
            tree->right = NULL;
            return;
    }
    else{
            // Makes sure the left and right pointers of the struct have
            // allocated space
            tree->left = calloc(1, sizeof(TreeType));
            tree->right = calloc(1, sizeof(TreeType));

            // Adds the yes and no branches to the tree, recursion will take care
            // of adding sub-branches
            treeBuilder(tree->left, phrase, level + 1);
            treeBuilder(tree->right, phrase, level + 1);
    }

    return;

}


TreeType InitTree (char *file){
    if(file == NULL){
            printf("File '%s' does not exist.\n", file);
            exit(2);
    }

    FILE *fp;
    fp = fopen(file, "r");

    // Create a space in memory for the loaded questions to occupy
    static char *phrases;
    phrases = (char *)malloc(MAXSTR * MAXNUMQS * sizeof(char));

    copyText(fp, phrases);

    fclose(fp);

    // Create space in memory for the tree structure
    TreeType tree;
    tree = (TreeType) calloc(1, sizeof(TreeType));

    // Create a pointer to a pointer so that treeBuilder can
    // change what the first pointer is pointing to, so the strings in
    // phrases can be added in order throughout the recursion
    static char *phrase_ptr, **phrase_ptr2;
    phrase_ptr = &phrases[0];
    phrase_ptr2 = &phrase_ptr;

    //Build the tree
    treeBuilder(tree, phrase_ptr2, 0);

    topNode = tree;

    return tree;
}

Sorry if this is tl;dr, but I wanted to be as clear as possible on my issue.

  • 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-01T10:04:00+00:00Added an answer on June 1, 2026 at 10:04 am

    Just one thing I noticed is that you’re using sizeof(TreeType), but TreeType is a pointer to a struct and not the struct itself. This means that you are creating a pointer that is pointing to nowhere, and that dereferencing the pointer will lead to undefined behaviour. Which having just read the rest of the question would certainly explain the segfaults.

    I think you would be better off not typedef-ing your struct as a pointer, and be more explicit with your use of pointers.

    eg.

    typedef struct treeStruct TreeType;
    
    void treeBuilder(TreeType *tree, char **phrase, long level){
        ...
        if (!tree->left) {
            // calloc returns a pointer to a new bit of memory that has been 
            // assigned on the heap
            TreeType *temp = calloc(1, sizeof(TreeType));
            // assignments below not explicitly needed as you're using calloc
            temp->string = NULL;
            temp->left = NULL;
            temp->right = NULL;
    
            tree->left = temp;
        }
        ...
    }
    

    Here’s a question about typedef-ing pointers. Seems to be relatively common in C, and used to imply that the data type is opaque and should not be dereferenced by the user (only by the API calls that the user passes it to).

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to render a haml file in a javascript response like so:
I have a text area in my form which accepts all possible characters from
I have a reasonable size flat file database of text documents mostly saved in
Basically, what I'm trying to create is a page of div tags, each has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

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.