Confusing problem with 'for' loops.
The code below is an extract of the main program, as I only need a portion of the program to recreate the problem I'm having.
If I run this code, the call to 'free' will crash, but only on specific cells, rather than all or none of them.
What further complicates the problem is on my desktop, the loop will crash on [0][1]. On my netbook, it will crash on [0][2]. Both do use slightly different compilers (netbook uses Dev-cpp 4.xxx, desktop uses Dev-cpp 5.xxx). Could it be Dev-cpp itself that's causing the problem?
Code:
#include <stdio.h>
struct cell
{
int value;
struct cell * next;
} sudokuArray[8][8];
initCell(struct cell * cellPtr, int number) // Create a linked list, containing numbers 1 to 9, in each array element.
{
if (number <= 8)
{
cellPtr->next = (struct cell *) malloc(sizeof(struct cell));
cellPtr->value = number;
number++;
initCell(cellPtr->next, number);
}
else
{
cellPtr->value = number;
cellPtr->next = NULL;
}
}
int main(void)
{
int a, b;
for (a = 0; a <= 8; a++) // Loop is simply meant to remove head off the linked list for array element.
{
for (b = 0; b <= 8; b++)
{
initCell(&sudokuArray[a][b], 1);
printf("Attempting to edit %i, %i\n", a, b);
struct cell * temp = &sudokuArray[a][b];
sudokuArray[a][b] = * sudokuArray[a][b].next;
free(temp);
}
}
getchar();
return 1;
}