0

I am currently doing lab 5:Inheritance on CS50.

I came across a interesting error. when i create a person object with the name person as follows,

//Create a new individual with generations
person *create_family(int generations)
{
    // TODO: Allocate memory for new person
    person* person = malloc(sizeof(person));
    if(person == NULL)
    {
        printf("Failed to malloc memory for person.");
    }

    // Generation with parent data
    if (generations > 1)
    {
        // TODO: Recursively create blood type histories for parents

            person->parents[0] = create_family(generations - 1);
            person->parents[1] = create_family(generations - 1);

        // TODO: Randomly assign child alleles based on parents
        for(int i = 0; i < 2; i++)
        {
            bool alleleMatch = false;

            do
            {
                char allele = random_allele();

                // check if either parents has this allele
                if (person->parents[i]->alleles[0] == allele || person->parents[i]->alleles[1] == allele)
                {

                    person->alleles[i] = allele;
                    alleleMatch = true;
                }
            } while (alleleMatch == false);
        }
    }

    // Generation without parent data
    else
    {
        // TODO: Set parent pointers to NULL

            person->parents[0] = NULL;
            person->parents[1] = NULL;


        // TODO: Randomly assign alleles
            person->alleles[0] = random_allele();
            person->alleles[1] = random_allele();

    }

    // TODO: Return newly created person
    return person;
}

i get the following error on valgrind.

running valgrind --show-leak-kinds=all --xml=yes --xml-file=/tmp/tmp0ux6kw_s -- ./inheritance...
checking that program exited with status 0...
checking for valgrind errors...
Invalid write of size 8: (file: inheritance.c, line: 87)
Invalid write of size 1: (file: inheritance.c, line: 91)
Invalid write of size 1: (file: inheritance.c, line: 92)
Invalid write of size 8: (file: inheritance.c, line: 59)
Invalid read of size 1: (file: inheritance.c, line: 71)
Invalid write of size 1: (file: inheritance.c, line: 74)
Invalid read of size 8: (file: inheritance.c, line: 71)
Invalid read of size 1: (file: inheritance.c, line: 138)
Invalid read of size 8: (file: inheritance.c, line: 140)
Invalid read of size 8: (file: inheritance.c, line: 113)

However, when i were to implement exactly that method with any other variable name such a p, i do not get any errors.

person *create_family(int generations)
{
    // TODO: Allocate memory for new person

    person* p = malloc(sizeof(person));
    if(p == NULL)
    {
        printf("Failed to malloc memory for person.");
    }

    // Generation with parent data
    if (generations > 1)
    {
        // TODO: Recursively create blood type histories for parents

            p->parents[0] = create_family(generations - 1);
            p->parents[1] = create_family(generations - 1);

        // TODO: Randomly assign child alleles based on parents
        for(int i = 0; i < 2; i++)
        {
            bool alleleMatch = false;

            do
            {
                char allele = random_allele();

                // check if either parents has this allele
                if (p->parents[i]->alleles[0] == allele || p->parents[i]->alleles[1] == allele)
                {

                    p->alleles[i] = allele;
                    alleleMatch = true;
                }
            } while (alleleMatch == false);
        }
    }

    // Generation without parent data
    else
    {
        // TODO: Set parent pointers to NULL

            p->parents[0] = NULL;
            p->parents[1] = NULL;


        // TODO: Randomly assign alleles
            p->alleles[0] = random_allele();
            p->alleles[1] = random_allele();

    }

    // TODO: Return newly created person
    return p;
}

i am aware i have solved this issue but what is the reason behind it occurring initially? Is there a reason why this error only occurs with the variable name person? Is it not recommended to create an person structure with the variable name person too?

You must log in to answer this question.

Browse other questions tagged .