How are array values stored in Little Endian vs. Big Endian architecture

Deadly 提交于 2019-12-06 04:25:45

In little-endian, the bytes are stored in the order least significant to most signficant. Big-endian is the opposite. For instance, short x=0x1234 would be stored as 0x34,0x12 in little-endian.

As already mentioned, it only affects the order of the bytes of a variable, and not the order of bits inside each byte. Likewise, the order of the elements of a C array are unaffected by endianness. array[1] always starts one sizeof(*array) after array[0].

However, I'm not sure if there is supposed to an indicator between values that represent an array in memory to show that it's a different element, like a null char.

There is no such indicator.

{01,23,45,FE,DC}

{45,23,01,DC,FE}

It would actually be

{00,00,00,00,00,01,23,45, 00,00,00,00,00,00,FE,DC}

and

{45,23,01,00,00,00,00,00, DC,FE,00,00,00,00,00,00}

because longs take 8 bytes.

Array elements in C are fixed size. That means that each element takes up exactly the number of bytes required by its type. In your example, x consists of two elements of type long. If long is 4 bytes on your computer, then each element takes up 4 bytes—no more, no less.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!