Search code examples
cstrict-aliasingdpdk

What does uintptr_t have to do with strict aliasing?


I was doing some research on strict aliasing and how to handle it and found this commit on DPDK.

To fix strict aliasing (according to the comments), they are casting the void* parameters src and dst into uintptr_t. And then using the casted versions.

In my understanding, this should do nothing with the strict aliasing rule since there is no mention of casting to uintptr_t in the rule itself.

Would a cast to uintptr_t really help strict-aliasing? Or would this just fix some possible warnings from GCC?


Solution

  • Would a cast to uintptr_t really help strict-aliasing?

    No, it would not.

    Or would this just fix some possible warnings from GCC?

    "Fix" in the sense of disguising the strict-aliasing violations well enough that the compiler does not diagnose them, yes, it might. And presumably it indeed did so for whoever made that change.

    This is pernicious, because now, not only may the compiler do something unwanted with the code, but you cannot even prevent it from doing so by passing it the -fno-strict-aliasing option (or whatever similar option a different compiler might provide). Worse, it might work fine with the compiler used today, but break months or years later when you upgrade to a new version or when you switch to a different C implementation.