1

For a while I've had this struct:

struct coordinates{
    struct coord coord_board[8][8];
};

And I've initialized it with

coordBoard = malloc(sizeof(struct coordinates));
for (col = 'a'; col <= 'h'; col++) {
        for (row = '1'; row <= '8'; row++) {
            initializeCoord(col, row);
        }
}

void  initializeCoord(char col, char row) {
    coordBoard->coord_board[col][row].Iter = 0;
    coordBoard->coord_board[col][row].occupant = NULL;
}

So this has actually worked for a while. I could even access them using chars too:

void printCoordBoard() {
    char col, row;
    printf("\n");
    for (col = 'a'; col <= 'h'; col++) {
        for (row = '1'; row <= '8'; row++) {
            struct coord l = board->coordBoard->coord_board[col][row];
            printf("%c ", l.occupant == NULL ? ' ' : l.occupant->type);
        }
        printf("\n");
    }
}

And this has also worked. But now it doesn't - And I have no idea what I've done to make it fail. Though when I think about it, it makes sense. It's been a while since I've used C, but I remember that all arrays are accessed using an integer - right? I initialize them as array[8][8]. So when I access it using e.g. 'a', I'd actually access location 97 (decimal value of char a), right? And then of course I'd have memory corruption.

My question is then: Why did it work? And even more weird - if I change the array to a [7][7], it works again... I'm getting confused. Another question is - would there be a way in C to use chars to access the data, with having to initialize an array of the highest char (in this case 'h', which would crate a 104x104 array)?

I hope someone can enlighen me!

Have a great day.

6
  • 2
    If it works, you got lucky. To get what you really want: add col=col-'a' and row=row-'1' inside the loop so the indices are normalized. Commented Nov 29, 2017 at 13:46
  • 2
    So this has actually worked for a while. Only for extremely loose definitions of "worked". Commented Nov 29, 2017 at 13:46
  • 1
    @nicomp To get what you really want: add col=col-'a' Until it's run with a non-ASCII character set... Commented Nov 29, 2017 at 13:46
  • Because it's undefined behavior. It's the silent killer of C programs. Commented Nov 29, 2017 at 13:47
  • Beware: 1. '1' is not the same as 1. 2. array indexes start at 0, not at 1 Commented Nov 29, 2017 at 13:51

1 Answer 1

4

Yes, you have major undefined behavior. You can't index like that, it makes no sense since you only have room for 8x8 elements and the integer value of characters is often larger than 0..7 which is your index space in each dimension.

It worked because the behavior of undefined behavior code is undefined, and "working as expected (for some value of expected)" is certainly part of the space of all that is undefined. As is anything else you can come up, basically. :)

In practice, it probably worked because memory allocation was sufficiently granular to make the major out-of-bounds accesses not fall anywhere you couldn't write. Perhaps you mangled malloc()'s book-keeping, but didn't notice because you never called free(), or malloc() again, or whatever.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. That's actually the first time I've had undefined behaviour actually working for this long - the code has been running for half a year. Good explanation!
@BenjaminLarsen be aware that "undefined behaviour" includes "apparently working fine".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.