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

I feel kinda confused reading the wording Alignment page #212

Open
iiian opened this issue Jan 13, 2024 · 2 comments
Open

I feel kinda confused reading the wording Alignment page #212

iiian opened this issue Jan 13, 2024 · 2 comments

Comments

@iiian
Copy link

iiian commented Jan 13, 2024

First and foremost, I appreciate you taking the time to make zig.guide, so thank you for being a maintainer.

Caveat: I could be completely wrong here, I'm just going from what I understand, and there's a good chance you know more than me, but I thought I'd mention anyway in case you agree.

Issue

https://zig.guide/working-with-c/alignment

The page here reads:

[1] For circuitry reasons, CPUs access primitive values at specific multiples in memory. This could mean, for example, that the address of an f32 value must be a multiple of 4, meaning f32 has an alignment of 4. This so-called "natural alignment" of primitive data types depends on CPU architecture. All alignments are powers of 2.

[2] Data of a larger alignment also has the alignment of every smaller alignment; for example, a value which has an alignment of 16 also has an alignment of 8, 4, 2 and 1.

[2] seems (to perhaps only me) to be contradictory to [1]: [2] seems to imply to me that an f32 can be stored at a memory address that is byte or even half-word aligned, in direct violation of what [1] is saying(?). I may be rusty on my CPU arch, but that's the wrong interpretation of what you wish to convey, right?

@iiian
Copy link
Author

iiian commented Jan 13, 2024

Furthermore, I'd like to suggest that the examples could do with perhaps either some comments or maybe diagrams of extra allocated memory or word/half-word partitioning (or whatever the best technical terms are)

@iiian
Copy link
Author

iiian commented Jan 13, 2024

And finally, I'm not following why with

const b1: u64 align(1) = 100;
const b2 align(1) = @as(u64, 100);

b1 needs the : but b2 doesn't 😵

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

1 participant