3 ms·
You can't cast a short ptr to an int ptr safely even ignoring aliasing rules. What bites you is that ints have different alignment than shorts (2 vs 4), so whil
by Asooka 8y ago
You can't cast a short ptr to an int ptr safely even ignoring aliasing rules. What bites you is that ints have different alignment than shorts (2 vs 4), so while this works on e.g. x86 almost always [1], your code won't always work on machines that can't do unaligned int reads.
There are a lot of sharp edges to C/++ and the community really is hurting for a good static analyzer that can at least save you from most of them.
[1] almost always, because while a simple unaligned mov is safe, if the compiler unrolls and vectorises your loop, it is free to then use aligned sse read instructions, assuming that your int ptr is aligned, because pointers must be aligned according to their type. Then your program crashes, but only in optimised builds.