hgm wrote:Karlo Bala wrote:
For example:
Code: Select all
int p = (ChessSquare*)board - (ChessSquare*)boards[0] -> 64
int p = (Board*)board - (Board*)boards[0] -> 1
So you are saying that I am mixing up pointers after all (although not within the expression): Although board and boards[0] are pointers, they are not (Board *), but they point to an 8-byte row of ChessSquare. That would explain the 8 (but not the 16).
I was pretty sure I got all multiples of 16 (but in the mean time I fixed it by writing
p = ((int) board - (int)boards[0]) / ((int) boards[1] - (int) boards[0]);
(This seemed the safest, given that I didn't understand what was going on).
I think that problem arise after passing array as function parameter.
boards[0], board[1],... are pointers to Board and working with 8x8 element memory blocks. Board is 8x8 array. Whenever you pass array through param it became pointer to n-1 right most dimension sub-array. So, after passing board[0] as board(param), board(param) became pointer to 8 element memory block. It seems that most of us got 8 as result of Apply(Board board) function, but in case of mixing pointers result should be undefined and could depend on compiler.
Here is compiler output (VS2005):
Code: Select all
004113BE mov eax,dword ptr [board]
004113C1 sub eax,offset boards (417178h)
004113C6 sar eax,5
004113C9 mov dword ptr [p],eax
Divide by 32 looks very strange to me and I haven't good explanation.
One way to get 8 as result:
Code: Select all
typedef ChessSquare Row[8];
int p = (Row*)board - (Row*)boards[0];
Maybe compiler made some similar conversion and we got 8, but 16? It is very strange...