0

I'm trying to do pset3/tideman. My question is, why when I run program, I get different result (matrix[m]) than run with debug50?

void lock_pairs(void)
{
    for (int i = 0; i < pair_count; i++)
    {
        int matrix[candidate_count];
        int matrix_sum = 0;
        for (int m = 0; m < candidate_count; m++)
        {
            for (int n = 0; n < candidate_count; n++)
            {
                if (locked[n][m] == true)
                {
                    matrix[m] = 1;
                }
            }
            printf("matrix %d = %d\n", m, matrix[m]);
        }
    }
}
~/pset3/tideman/ $ ./tideman a b c
[...]
matrix 0 = 0
matrix 1 = 0
matrix 2 = -1382903440
[...]
~/pset3/tideman/ $ debug50 tideman a b c                                                                                                                                                            
[...]
matrix 0 = 0
matrix 1 = 0
matrix 2 = 0
[...]
1
  • You code might have memory corruption. Can you post the whole code? You can also check it by following this instruction.
    – stensal
    Jun 1 '20 at 17:19
3

Declaring the array here int matrix[candidate_count]; allocates the memory for it. But it does not initialize the contents. At the end of the for loop, there is a good chance that matrix[m] has not been set to anything. The difference between the debug50 result and the program is result is because the contents of the assigned memory addresses is different.

You must log in to answer this question.

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