What a Single uint32_t Teaches About C Types and Memory

https://hackernoon.imgix.net/images/b2pTDoUt6IdXJiC9XI1pkUW5MJy1-fp13e0i.png

Disclosure: I'm the author and maintainer of the open-source repo referenced in this piece. It's free, MIT-licensed, and I don't monetize it in any way. This piece was originally published on dev.to; this version has been reworked for HackerNoon's house style, and revised further based on technical feedback from readers of the original post.

What a Single uint32_t Taught Me About Types, Endianness, and Alignment

The question that started this

The first time I wrote int x = 42;, I pictured a box labeled "integer," sitting somewhere in memory, holding the number 42 the way a jar holds marbles.

That mental model is useful. It's also incomplete. Your CPU doesn't store an "integer" - it stores a pattern of bits, and separately, some instruction decides to treat that pattern as a signed number. Change the instruction, and the exact same bits can mean something else entirely.

I wanted to see...

Copyright of this story solely belongs to hackernoon.com. To see the full text click HERE

Read more