简体   繁体   English

通过无符号整数读取无符号字符数组是不确定的,因此不安全吗?

[英]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. 使用OR左移绝对不是我想要的。 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. 编辑:是的,我正在读n设定的值之后rbg和不是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. 这是C ++的组成部分之一,比遵循标准更容易检查编译器在做什么。 As far as I know most compilers still do what you expect (MSVC, GCC, CLANG). 据我所知,大多数编译器仍然可以完成您期望的操作(MSVC,GCC,CLANG)。 Are you thinking something esoteric? 你在想什么深奥的东西吗?

In the following link the Sun C++ compiler is mentioned as not working: 在以下链接中,提到了Sun C ++编译器不起作用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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