39 ms·
> [You probably found the bug, right ? If not: the obvious issue is that 'struct ip_weight' needs padding for the second field. No, the bug is thinking that ha
by jcelerier 8y ago
> [You probably found the bug, right ? If not: the obvious issue is that 'struct ip_weight' needs padding for the second field.
No, the bug is thinking that hashing random bytes in your memory is correct. Why wouldn't you make a correct hash function for your struct ?!
- kjeetgill 8y agoI really have to second this one. It's still a rough point against C, but if padding isn't treaded as "semantically unreachable" idiomatically that IS the real gotcha. The idiom here is to take the address of the struct and read the width of it's whole footprint in memory not just the field data. It's a weak idiom breaking under a common use case.
- Too 8y agoMaybe this is the biggest mistake of C, allowing users to access underlying raw memory so easily and misleading people with "convenient" functions like memcmp etc ? Most "UB bugs" stem from users who think they know that a struct or data type will be laid out in a certain sequence in memory.
- tonysdg 8y agoUnless I'm mistaken, the low-level acess to memory is one of the defining features of C. It's basically designed to be human-readable assembly (which is just human-readable machine code). If anything, I'd blame compilers here -- IMO, they should automatically throw at least a warning any time they need to pad/rearrange a struct to make it explicitly clear to developers what's happening.
- Too 8y agoAccess to low level memory such as registers is always explicitly requested with the volatile keyword. All other memory is implementation details. C is far from being a human readable assembly and has never been, except accidentally. Even the old language spec from 1989 talks about an "abstract machine with the expressions being evaluated as specified by the semantics". One of the first chapters 1.2 Scope makes this very clear and then 2.1.2.3 Program execution clarifies this even further. Other things to note is that the spec doesn't mention variables being ever stored on the stack, instead it's called automatic storage, see "Storage durations of objects", the word stack is not even mentioned once throughout the whole spec. Today with modern CPUs all this is even more important, if you think you are operating on a global memory array you are doing it wrong.