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

Recommended Answers

All 5 Replies

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).

AIstruc_word

Add a constructor which initializes the two pointers to NULL

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.

Thank-you Banfa & Ancient Dragon.

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

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)
    {
        ...
        ...
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.