Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
DorpsGek opened this issue Jan 24, 2015 · 3 comments
Closed

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

DorpsGek opened this issue Jan 24, 2015 · 3 comments
Labels
flyspray This issue is imported from FlySpray (https://bugs.openttd.org/)

Comments

@DorpsGek
Copy link
Member

bcmpinc opened the ticket and wrote:

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 #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
};

Reported version: trunk
Operating system: All


This issue was imported from FlySpray: https://bugs.openttd.org/task/6218
@DorpsGek
Copy link
Member Author

planetmaker wrote:

I count 6 bytes and one uint16 which is two bytes, thus the total is eight bytes. Where is now the issue?


This comment was imported from FlySpray: https://bugs.openttd.org/task/6218#comment13744

@DorpsGek
Copy link
Member Author

bcmpinc wrote:

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.

Attachments


This comment was imported from FlySpray: https://bugs.openttd.org/task/6218#comment13745

@DorpsGek
Copy link
Member Author

planetmaker closed the ticket.

Reason for closing: Fixed

In r27126. Nice find. Thanks for the report and demonstration.


This comment was imported from FlySpray: https://bugs.openttd.org/task/6218

@DorpsGek DorpsGek added Core flyspray This issue is imported from FlySpray (https://bugs.openttd.org/) labels Apr 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flyspray This issue is imported from FlySpray (https://bugs.openttd.org/)
Projects
None yet
Development

No branches or pull requests

1 participant