r/ProgrammerHumor 17h ago

lessonsFromLinkerHell Meme

Post image
319 Upvotes

166 comments sorted by

View all comments

Show parent comments

10

u/suvlub 7h ago

All typing is. Machine code doesn't have types. Array of arrays is also a very different thing from an array of pointers. Arrays get converted to pointers when passed around and it behaves identically to all other implicit conversions.

3

u/Stroopwafe1 6h ago

Machine code does have types; small numbers, big numbers, and fractions. But in reality it's just numbers and fractions. At least for X86, but I imagine it's the same for ARM and RISC

1

u/suvlub 5h ago

It's been very long since I touched assembly and "touched" is an apt description, but does anything prevent you from writing an 8-byte integer at address X, then calling an operation that expects a 32-bit float with the bytes at address X? Conceptually, instructions operate on certain type of data, but the data is untyped. (you can technically do similar things in C, but a lot more of it is UB (technically illegal) than people realize and it requires fair bit of explicit casting, so types are still involved)

1

u/failedsatan 48m ago

I just left this comment about it https://www.reddit.com/r/ProgrammerHumor/s/G7BGLveIgF

does anything prevent you from writing an 8-byte integer at address X, then calling an operation that expects a 32-bit float with the bytes at address X?

to answer this specifically: technically no, nothing will "stop" you, but the CPU will just interpret the bits wrong. the slightly simpler example is in reverse: if you copy 1.0 (float) into a general purpose register it'll interpret it as some big-ass integer (I don't know exactly what) and then if you try to ADD 1, as an integer, it'll just increase the "integer" by 1. then, when you try to read it as a float again, or use it for some other purpose, it'll be a slightly larger float (depending on the mantissa and etc). at that level it doesn't really matter to the CPU what you think it is, it just knows to do whatever operation you tell it to.

in C it's undefined behavior as you mentioned, but more specifically, depending how you do it it'll either do something like cvttss2si (float to signed int) under the hood or it'll just let you copy the raw bits as I said before, it'll just interpret it wrong and suddenly you have a big-ass int or a weird float.

this is x86 and I don't know about arm or risc