简体   繁体   中英

Is reading an unsigned char array through an unsigned integer undefined and thus not safe?

I read this question and got extremely confused on what I have to do with my code. If my reading is correct, my code below is undefined according to the current standard. Below is everything I need, and I really cannot find a better way to do this. Using left shift with OR is definitely not what I want. Is it just fine to leave it this way?

EDIT: And yes I am reading n after setting the values of rbg and not n.

union Pixel {
    unsigned char rgb[4];
    uint32_t n;

    Pixel():
    n(0) {}
};

This is one of the parts of C++ where it is easier to check what your compiler is doing than following the standard. As far as I know most compilers still do what you expect (MSVC, GCC, CLANG). Are you thinking something esoteric?

In the following link the Sun C++ compiler is mentioned as not working:

http://blog.regehr.org/archives/959

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM