Sobeston / zig.guide

Repo for https://zig.guide content. Get up to speed with Zig quickly.
https://zig.guide
MIT License
652 stars 170 forks source link

I feel kinda confused reading the wording Alignment page #212

Open iiian opened 8 months ago

iiian commented 8 months ago

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 commented 8 months ago

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 commented 8 months ago

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 😵