0

I am having some difficulty's locating an issue within some code I am working on.
It breaks when it runs the "if (AIwords->name == NULL)"

    // This is the struct which is located in the header(.h)
    struct AIstruc_word
    {
        char *name;
        bool controlStatement;
        struct AIstruc_word *next;
    };

    // This is the struct that contains the sub struct
    struct AIstruc_sentence
    {
         struct AIstruc_word word;
         ....
         ....
         ....

    // This is the function which is crashing the program
    int AI_sentenceProc(struct AIstruc_sentence *AIdatabase, char *word)
    {
        struct AIstruc_word *AIwords = (struct AIstruc_word*)&AIdatabase->word;
        ...
        ...
        ...
        for (; ; AIwords = AIwords->next)
        {
            printf("%d\r\n", iPosition++);
            /// When we reach an empty word slot
            if (AIwords->name == NULL)
            {
                /// Break out of this 'do{}while()' loop.
                break;
            }
            if (AIwords->next == NULL)
            {
                AIwords->next = (struct AIstruc_word *)calloc(1, sizeof(struct AIstruc_word));
                break;
            }
        }
        ...
        ...
        ...

        // This is the code that passes the parent struct
        int AI_sentenceRequestProcess(.....)
        {
            struct AIstruc_sentence AIdatabase;
            ....
            ....
            AI_sentenceProc(&AIdatabase, quickBuffer);

Does anyone have an idea what I'm doing wrong here....
Help would be greatly appreciated

4
Contributors
5
Replies
7
Views
5 Years
Discussion Span
Last Post by ravenous
0

Most likely cause is that AIwords is either NULL or invalid so when you dereference it to get the name member you get a SIGSEGV (memory error).

0

The problem is I have declared the AIstruc_word struct within the ASstruc_sentence structure, it is not a pointer so surely the first one should be allocated.... I'm thinking that shomehow struct AIstruc_word *AIwords = (struct AIstruc_word*)&AIdatabase->word; is wrong and it is pointing to an invalid pointer which means if I am putting data in it. It will not be put into the global structure but a local calloc'd struct.

0

Thank-you Banfa & Ancient Dragon.

I used memset(AIdatabase, '\0', sizeof(AIstruc_sentence)); in the calling function.

0

Just a small point but, in C++, you don't need to put the struct keyword before the typename when you're using your structs. Also, I don't think that you need to do all the casting that you're doing. I think that this would be fine:

struct AIstruc_word
{
    // Set the name pointer to NULL on construction
    AIstruct_word() : name( NULL ) {}

    char *name;
    bool controlStatement;
    struct AIstruc_word *next;
};

// This is the struct that contains the sub struct
struct AIstruc_sentence
{
     // Don't need `struct` here
     AIstruc_word word;
     ....
     ....
     ....
};

// This is the function which is crashing the program
int AI_sentenceProc(struct AIstruc_sentence *AIdatabase, char *word)
{
    // No `struct` and no cast
    AIstruc_word *AIwords = &AIdatabase->word;
    ...
    ...
    ...

    // Use a `while` here
    while ( AIwords = AIwords->next)
    {
        ...
        ...
    }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.