简体   繁体   English

是什么让这段代码“endian独立”?

[英]What makes this code “endian independent”?

I came across the following code, and was told that it means that COL_8888_RED is "endian independent". 我遇到了以下代码,并被告知这意味着COL_8888_RED是“endian independent”。 Why? 为什么? What makes this endian independent? 是什么让这个端点独立? (I have asked the original coder but they're not getting back to me ... heck maybe they don't know either.) (我问过原来的编码员,但他们没有回复我...... 他们也许也不知道。)

union _colours {
    uint8  c[3][4];
    uint32 alignment;
};

static const union _colours col_8888 = {
        {    /*   B     G     R     A  in memory     */
                {    0x00, 0x00, 0xFF, 0xFF, }, /* red   */
                {    0x00, 0xFF, 0x00, 0xFF, }, /* green */
                {    0xFF, 0x00, 0x00, 0xFF, }, /* blue  */
        }
};

#define COL_8888_RED   *((uint32 *)&col_8888.c[0])

This code is not "endian-independent" in a sense that platforms with different endianness will give you different values seen through COL_8888_RED . 在某种意义上,具有不同字节序的平台将为您提供通过COL_8888_RED看到的不同值,此代码不是“字节序独立的”。 In other words, in the traditional understanding of endian-dependency this code is as endian-dependent as it ever gets. 换句话说,在对endian-dependency的传统理解中,这个代码依赖于endian依赖。

A different question is where is that COL_8888_RED is supposed to be used. 另一个问题是应该使用COL_8888_RED位置。 Maybe it is intended to be passed to some API, which by itself is endian-dependent in the very same way to the point that the endian-dependency of the API cancels out the endian-dependency of COL_8888_RED . 也许它的目的是传递给某些API,它本身依赖于字节序依赖于API的字节序依赖性取消了COL_8888_RED的字节序依赖性。 In that case everything will work "as intended", ie endian-independently. 在这种情况下,一切都将“按预期”工作,即字节独立。 (For example, if the API receives the color value as uint32 and then separates it into the ARGB components by using the same union, it will get the correct original ARGB values regardless of endianness.) (例如,如果API接收颜色值为uint32 ,然后使用相同的联合将其分离为ARGB组件,则无论字节顺序如何,它都将获得正确的原始ARGB值。)

But nevertheless, to say that the value of COL_8888_RED by itself is endian-independent is totally incorrect. 但是,要说COL_8888_RED本身的值与字节无关是完全错误的。

Arrays of bytes are endianness independent on almost all architectures. 字节数是与所有体系结构无关的字节序。 By assigning to it as a byte array, the programmer ensures consistent content for the data. 通过将其指定为字节数组,程序员可确保数据的一致内容。 The bytes can also be accessed individually on any architecture. 也可以在任何架构上单独访问这些字节。 If the coder had just made an array of 3 words, machine endianness would play a role in determining the exact layout of the bits. 如果编码器刚刚制作了3个字的数组,则机器字节序将在确定位的确切布局中起作用。

I suppose that COL_8888_RED, as a macro, will always be a uint32, which, as long as the macro COL_8888_RED is always used, the source byte array {0x00,0x00,0xFF,0xFF} will always translate into what the programmer wants to mean as RED. 我认为作为宏的COL_8888_RED将始终是一个uint32,只要总是使用宏COL_8888_RED,源字节数组{0x00,0x00,0xFF,0xFF}将始终转换为程序员想要的意思作为RED。

The definition, then, means you can write the same source code on a big endian, or little endian, machine, and go from a discrete array to a logical colour. 因此,定义意味着您可以在大端或小端机器上编写相同的源代码,并从离散数组转换为逻辑颜色。

EDIT: why then, not use an enumeration, or a constant like "1"? 编辑:为什么然后,不使用枚举,或像“1”这样的常量?

Probably, the original API developer wanted to be able to point to another location of {0x00,0x00,0xFF,0xFF} in memory, so that code like the following could be written: 可能原始API开发人员希望能够在内存中指向{0x00,0x00,0xFF,0xFF}的另一个位置,以便可以编写如下代码:

uint8 *p = malloc( sizeof(uint8)*4 );

fread( p, sizeof(uint8), 4, inBuff );

if( *((uint32 *)p) == COL_8888_RED )
{
    printf( "I read red!\n" );
}

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

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