I'm getting a segmentation fault on my code for Lab 5. Debug50 says line 57 is to blame, and when I look at the info, it looks like parent0 and parent1 are both empty? (If I'm understanding 0x0 right?) Screenshot included: Image depicts debug50 error screen. Highlights line 57, says "Exception has occurred. Segmentation fault." Under variables, locals, parent0 and parent1 are both 0x0.

Am I on the right track seeing that parent0 and parent1 are both empty, so the code is not working? If so, how do I go about fixing this? Actual code included for readability:

    person *create_family(int generations)
    //Allocate memory for new person
    person *p = malloc(sizeof(person));

    // If there are still generations left to create
    if (generations > 1)
       // Create two new parents for current person by recursively calling create_family
        person *parent0 = create_family(generations - 1);
        person *parent1 = create_family(generations - 1);

        // TODO: Set parent pointers for current person
        p->parents[0] = parent0;
        p->parents[1] = parent1;

        // TODO: Randomly assign current person's alleles based on the alleles of their parents
        p->alleles[0] = parent0->alleles[rand() % 2]; //line 57
        p->alleles[1] = parent1->alleles[rand() % 2];

1 Answer 1


Found it! I forgot to modify the return statement for create_family().

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .