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

Misunderstanding in data structure alignment #33

Closed
mightymatth opened this issue May 6, 2020 · 1 comment
Closed

Misunderstanding in data structure alignment #33

mightymatth opened this issue May 6, 2020 · 1 comment

Comments

@mightymatth
Copy link

// Rule 1:
// Depending on the size a particular value, Go determines the alignment we need. Every 2 bytes
// value must follow a 2 bytes boundary. Since the bool value is only 1 byte and start at
// address 0, then the next int16 must start on address 2. The byte at address that get skipped
// over becomes a 1 byte padding. Similarly, if it is a 4 bytes value then we will have a 3
// bytes padding value.

The first rule's statements are really hard to comprehend if it's your first time dealing with data structure alignments. Please, correct me if I'm wrong.

Every 2 bytes value must follow a 2 bytes boundary.

It is really hard sentence to start this explanation, because it does not imply what to do with other cases, e.g. if you put 1 byte value, it must be followed by 1 byte boundary, etc. The reader may not conclude that this implies even further for 2, 4, 8 byte values (I'm not sure for Go case, but this statement may depend on specific architecture).

Similarly, if it is a 4 bytes value then we will have a 3 bytes padding value.

Here, you don't point out that you are referring on the same example with setting 1 byte value on the beginning of the 8 byte block. I was thinking that you just appended value of pi to complete explaining alignment in that specific struct

I don't say that something here is wrong, but it's hard to understand. Maybe it should be revisited or put some references like this to be easier to understand.

@hoanhan101
Copy link
Owner

Thanks for looking into this. I'm closing this as a no-op for now though feel free to open a pull request if needed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants