FS#6218 - sizeof(Tile) == 10 rather than the expected 8 bytes.

Attached to Project: OpenTTD
Opened by Bauke (bcmpinc) - Saturday, 24 January 2015, 13:57 GMT
Last edited by Ingo von Borstel (planetmaker) - Monday, 26 January 2015, 10:29 GMT
Type Bug
Category Core
Status Closed
Assigned To No-one
Operating System All
Severity Medium
Priority Normal
Reported Version trunk
Due in Version Undecided
Due Date Undecided
Percent Complete 100%
Votes 0
Private No


The Tile data type has a size of 10 bytes, even though the commit log suggests that it should be 8 bytes. This is caused by the 2-byte member 'm2', which is on an alignement boundary, as is shown below. As a result, the map wastes 2 bytes per tile. This was introduced by commit 26873 and  FS#6510  tried to reduce the size of Tile back to 8 bytes, which failed due to the position of m2 in the struct.

The current (r27120) definition of Tile in map_type.h:

struct Tile {
byte type; ///< The type (bits 4..7), bridges (2..3), rainforest/desert (0..1)
byte height; ///< The height of the northern corner.
byte m1; ///< Primarily used for ownership information
uint16 m2; ///< Primarily used for indices to towns, industries and stations
byte m3; ///< General purpose
byte m4; ///< General purpose
byte m5; ///< General purpose

This task depends upon

Closed by  Ingo von Borstel (planetmaker)
Monday, 26 January 2015, 10:29 GMT
Reason for closing:  Fixed
Additional comments about closing:  In r27126. Nice find. Thanks for the report and demonstration.
Comment by Ingo von Borstel (planetmaker) - Sunday, 25 January 2015, 18:52 GMT
I count 6 bytes and one uint16 which is two bytes, thus the total is eight bytes. Where is now the issue?
Comment by Bauke (bcmpinc) - Sunday, 25 January 2015, 19:40 GMT
After m1 the compiler will insert a dummy-byte, to ensure that m2 is properly aligned. After m5 another dummy byte is added also for alignment purposes. It is probably best demonstrated with a test program. So I've attached a small program that you can compile and run to see that the current definition of Tile takes up 10 bytes (and thus is wasting 2 bytes). The problem can be fixed by moving m2 above m1.