Detecting obvious endian issues


Consider the following code:

int main(int argc, char *argv[])
    unsigned int a = 0xdeadbeef;
    unsigned char *b = (unsigned char *) &a;

    printf("%x %x %x %x\n", b[0], b[1], b[2], b[3]);
    return 0;

Is there a gcc/clang (or any other compiler!) warning that I can enable that will tell me that this code will behave differently on big and little endian systems? I tried -Wall with gcc but it doesn't warn.

Of course implementing such a warning would involve deeper code analysis because in order to warn the compiler would have to realize that b is set to a pointer of a and then its individual bytes are accessed. But given the powerful code analysis and optimization skills of modern compilers, I'd expect such things to be possible.

FWIW, I know how to write endian-neutral code but apparently since big endian CPUs are becoming more and more obsolete many people don't seem care much about it any more and so I often have to fix lots and lots of endian issues when using components written for little endian on big endian systems.

I know it is pretty much impossible for the compiler to detect all endian issues but I think some, like the issue shown above, could be detected so that's why I'm wondering whether there are any warnings that could help me to detect endian issues. Or maybe there are some other tools that I could use to detect those issues?

asked on Stack Overflow Mar 20, 2018 by Andreas

1 Answer


Even though theoretically possible, it would probably be impractical for compilers to implement this feature. If you're looking for some way to automate checking for endianness issues, you will have to resort to using a static analyzer but even they would probably not do it to the level that you'd want.

answered on Stack Overflow Mar 20, 2018 by mnistic

User contributions licensed under CC BY-SA 3.0