Will the char array differ in order in a system with a small number or a large endian

I have an array char c[12] = {'a','b','c','d','e','f','g','h','0','1','2','3'} In hexadecimal, these values ​​will be {0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x30, 0x31, 0x32, 0x33}

I was wondering if the array will be stored in memory differently in a system with a large ented or small entity?

I thought they would be the same because on endian systems it works by determining how to store an element by the least or most significant bit of one element in an array and sort bytes, but since char is just one byte they arrange bytes in the same way .

+5
source share
4 answers

Think about what “big endian” and “little end” mean.

Memory is a sequence (or on more interesting machines, several sequences) of bytes. For elements whose size is two or more bytes, the bytes of the element can be stored in memory in different orders, and the two most obvious orders are called the "big end" and "small end" (if you have three or more bytes, that is, more than two possible byte orders, and in any case, who says that int bits, for example, cannot be distributed across several bytes in a chaotic but fixed order?)

But for a sequence of bytes, the byte order is the byte order. The "big end" and "small end" are determined by the fact that the bits of the larger element are located in the byte array. For a sequence of bytes, they are stored in the order in which they are stored - there is no way for any other order. It’s like asking “could the numbers 1, 2, 3, 4 be something other than the numbers 1, 2, 3, 4?”, And, of course, they cannot.

+9
source

char / byte is, by definition, the smallest addressable unit of memory. Therefore, in one byte there is no meaningful concept of "endianness", because the processor cannot index inside it; he can read only everything. The individual bits from the byte are obtained by mathematical operations, not by addressing; their physical location has nothing to do with how they are read. Endianness refers only to multi-byte objects that have indexable and therefore physically ordered subcomponents.

By definition, an array has elements in only one order, otherwise indexing operations will fail, therefore machine concepts of endianness for larger types that could hypothetically occupy the same space also have nothing to do with this, since they will destroy access to the rule array.

+4
source

Bytes must have upstream memory addresses (as you can see from your C program) in the same order in which you defined them.

+2
source

char arrays are the same on any intelligent system you will ever encounter. Its multi-byte data types that have a different order. In the past, there were systems with an odd concretization of bits, but I don't think they are already made.

+1
source

Source: https://habr.com/ru/post/1205057/


All Articles