5

I am inquiring about how to tell when one element in an array has finished and another is beginning in an endian architecture.

I have 2 arrays where the size of long is 8 and the size of char is 1

long x[2] = {0x012345,0xFEDC};

char c[12] = {'a','b','c','d','e','f','g','h','0','1','2','3'};

And I was wondering how these values would be stored in the different Endian architectures if we consider x starting at memory address 0x100 and c starting at memory address 0x200.

I thought that the Big Endian address would be {01,23,45,FE,DC} where the first element of the set is at memory address 0x100, the next is 0x101, third is 0x102, and so on since it stores the values based on the MSB being first. However, I'm not sure if there is supposed to be an indicator between values that represent an array in memory to show that it's a different element, like '\0' (null char). like {01, 23, 45,'\0', FE, DC}

Likewise for the Little Endian Architecture I believe it would store it as {45,23,01,DC,FE}, but I am not sure whether there should be some indicator to highlight the different elements in the array

10
  • You have an array of longs. A long will typically be the word size of the platform, so 4 or 8 bytes. So why would two longs total 5 bytes of memory usage, with each using only bytes with significant digits? How could a computer ever possibly access this in a sane manner? Commented Oct 19, 2014 at 22:16
  • @Sanhadrin: I don't believe "sane" is the correct way to state it. At the very worst, it could simply be integrated into a compiler. But there is no reason for null separating bytes or otherwise indicate the length, because the size of each element is implicitly stated by its type. Commented Oct 19, 2014 at 22:44
  • @Sanhadrin "long will typically be the word size of the platform" is an over generalization - many counter examples exist. Commented Oct 19, 2014 at 22:53
  • @Jongware Considering it violates the standard, results in unaligned accesses, and would require significantly more overhead for tracking associated indices, insertions, and deletions, it's not sane in any way. Commented Oct 19, 2014 at 22:55
  • @Sanhadrin: But that's because C is designed to be 'close to the metal'. If the 'metal' were any different, a 5-byte type could make sense. Over the years, data type size has increased from 2 to (what?) 10 or 12 bytes -- in the same standard (of which there are actually lots). If there would be a 5-byte type, there is still no need for a padding byte, as having it would violate yet other parts of the standard(s). Commented Oct 19, 2014 at 23:00

2 Answers 2

11

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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.

1 Comment

In this case the size of long is 8 bytes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.