Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does endianness have an effect when copying bytes in memory?

Tags:

c++

c

memory

Am I right in thinking that endianess is only relevant when we're talking about how to store a value and not relevant when copying memory?

For example

if I have a value 0xf2fe0000 and store it on a little endian system - the bytes get stored in the order 00, 00, fe and f2. But on a big endian system the bytes get stored f2, fe, 00 and 00.

Now - if I simply want to copy these 4 bytes to another 4 bytes (on the same system), on a little endian system am I going to end up with another 4 bytes containing 00, 00, fe and f2 in that order?

Or does endianness have an effect when copying these bytes in memory?

like image 411
BeeBand Avatar asked Dec 02 '22 05:12

BeeBand


2 Answers

Endianness is only relevant in two scenarios

  1. When manually inspecting a byte-dump of a multibyte object, you need to know if the bytes are ordered in little endian or big endian order to be able to correctly interpret the bytes.
  2. When the program is communicating multibyte values with the outside world, e.g. over a network connection or a file. Then both parties need to agree on the endianness used in the communication and, if needed, convert between the internal and external byte orders.
like image 100
Bart van Ingen Schenau Avatar answered Dec 04 '22 20:12

Bart van Ingen Schenau


Answering the question title.

Assume 'int' to be of 4 bytes

union{
   unsigned int i;
   char a[4];
};

// elsewhere
i = 0x12345678;
cout << a[0];   // output depends on endianness. This is relevant during porting code
                // to different architectures

So, it is not about copying (alone)? It's about how you access?

It is also of significance while transferring raw bytes over a network!.

Here's the info on finding endianness programatically

like image 45
Chubsdad Avatar answered Dec 04 '22 20:12

Chubsdad