cassemblyx86stackendianness

Does little endian impact arrays?


given the following command in C language:

unsigned char myID[10] = "123456789"

And given the fact I'm using little endianness with x86 architecture, how would that be saved in memory?

is it:

Address     | Value
----------------------
0x10b       | 0x00   ('\0')
0x10a       | 0x00   ('\0')
0x109       | 0x00   ('\0')
0x108       | 0x39   ('9')

0x107       | 0x38   ('8')
0x106       | 0x37   ('7')
0x105       | 0x36   ('6')
0x104       | 0x35   ('5')

0x103       | 0x34   ('4')
0x102       | 0x33   ('3')
0x101       | 0x32   ('2')
0x100       | 0x31   ('1')

or the opposite way around? I know that in little endianness given 0x0102 then lower bytes (02) in this case gets saved in lower memory, but I'm not sure if this is the same for arrays.

Plus to make it multiplication of 4 are the bytes added to the end like I did or the opposite way?


Solution

  • Array elements are always stored in the order of their indices. Endianness is something that affects the bytes in an individual variable that is not an array or struct.

    As as example, suppose you have the following:

    uint16_t arr[4] = { 1, 2, 3, 4 };
    

    On a little endian machine, the bytes of the array would be as follows:

    ---------------------------------------------------------
    | 0x01 | 0x00 | 0x02 | 0x00 | 0x03 | 0x00 | 0x04 | 0x00 |
    ---------------------------------------------------------
    

    While on a big endian machine, it would look like this:

    ---------------------------------------------------------
    | 0x00 | 0x01 | 0x00 | 0x02 | 0x00 | 0x03 | 0x00 | 0x04 |
    ---------------------------------------------------------