简体   繁体   中英

C Dynamic Memory Segmentation Fault

I'm completing this assignment for school and was wondering why I keep receiving segmentation fault for my dynamic memory allocation for a finite state machine. I've created identical memory allocation and free functions, however I still receive a segmentation fault even if my main only includes them one after each other. I was wondering if anyone could give me a hand? Thank you:)

Main function:

int main(int argc, char** argv) {
    
    if(argc < 2 || argc > 2 || atoi(argv[1]) > MAX_STATE_MACHINE_NO)
    {
        printf("Error: Invalid input arguments\n");
        exit(1);
    }

    int stateMachineNumber;

    stateMachineNumber = atoi(argv[1]);
    StateMachine** stateMachines = malloc(sizeof(StateMachine*) * stateMachineNumber);

    stateMachineAllocation(stateMachines, stateMachineNumber);
    //grabInputs(stateMachines, stateMachineNumber);
    freefunction(stateMachines, stateMachineNumber);


    return 1;
}

Memory allocation function:

void stateMachineAllocation(StateMachine** stateMachines, int stateMachineNumber)
{

    for(int i = 0; i < stateMachineNumber; i++)
    {   
        stateMachines[i] = (StateMachine*)malloc(sizeof(StateMachine));
        stateMachines[i]-> states = malloc(sizeof(State)*MAX_STATE_NO);

        for(int j = 0; j < MAX_STATE_MACHINE_NO; j++)
        {
            stateMachines[i]-> states[j] = (State*)malloc(sizeof(State));

            for(int w = 0; w < 4; w++)
            {
                stateMachines[i]-> states[j]-> connectedStates[w] = (State*)malloc(sizeof(State));
            }
        }
    }
}

Free memory function:

void freefunction(StateMachine** stateMachines, int stateMachineNumber)
{

     for(int i = 0; i < stateMachineNumber; i++)
    {
        for(int j = 0; j < MAX_STATE_NO; j++)
        {
            for(int w = 0; w < 4; w++)
            {
                free(stateMachines[i]-> states[j]-> connectedStates[w]);
            }

            free(stateMachines[i]-> states[j]);
        }

        free(stateMachines[i] -> states);
        free(stateMachines[i]);
    }

    free(stateMachines);
}

Thank guys, I'm relatively new to C so this would be a big help.

One thing that does not look right is:

StateMachine** stateMachines = malloc(sizeof(StateMachine*) * stateMachineNumber);

If you intend to have a block of stateMachines, you probably should use:

StateMachine* stateMachines = malloc(sizeof(StateMachine*) * stateMachineNumber);

This will return the memory address of a number of state machines, one after the other in memory (effectively an array of state machines).

You are assigning this currently to something that the compiler interprets as a pointer to a pointer to the first stateMachine, which is probably not what you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM