Hacker News new | past | comments | ask | show | jobs | submit login

This is not UB.

In this case where you're using the wrong pointer type the worst thing that can happen is you can have aliasing issues. For example, if I have an int* and a double*, the C compiler is free to assume that the pointers can never alias each other so it can optimize loads and stores assuming that an update to the int pointer will never affect the double pointer. In general this can cause problems if the pointers actually do alias each other, but if there's no aliasing occurring then nothing bad will happen.




My understanding was that the UB was defined such that:

    int i = 0;
    float f = 1.0f;
    void* p;
    
    if(rand() & 1)
      p = &i;
    else
      p = &f;
    
    int j = *(int*)p;
    assert(i == j);
could legally be compiled to a no-op (or a single call to rand() with an ignored result, I suppose), despite there being no "weird" pointer stores, since the ill-typed-at-runtime load "can't happen," so the compiler can reason that (int*)p must equal &i, and hence j = i.

EDIT: And, in fact, gcc does this: https://godbolt.org/z/WM33vzzdo

Not sure if that applies to vector loads, and the gcc docs don't specify, though.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: