Does endianness make sense when copying bytes to memory?

I understand correctly that endianess only matters when we talk about how to store a value and not matter when copying memory?

for instance

if I have a value 0xf2fe0000 and save it in the system a little late - the bytes will be stored in order 00, 00, feand f2. But in a large system, the system stored bytes f2, fe, 00and 00.

Now, if I just want to copy these 4 bytes to the other 4 bytes (in one system), in a little endian system, I'm going to eventually add another 4 bytes containing 00, 00, feand f2in that order?

Or does endiedness have an effect when copying these bytes into memory?

+3
source share
7 answers

memcpy does not know what it is copying. If he needs to copy 43 61 74 00, he does not know if he is copying 0x00746143 or 0x43617400 or float or"Cat"

+3
source

Specificity applies only in two scenarios.

  • When manually checking the byte dump of a multibyte object, you need to know if the bytes are ordered in entity or large entiant order in order to be able to interpret the bytes correctly.
  • When a program transfers multibyte values ​​to the outside world, for example, over a network connection or file. Then, both parties must agree on the consistency used in the message, and, if necessary, convert between internal and external byte orders.
+7

.

, 'int' 4

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

, ( )? , ?

!.

+4

, , endianess, endian-

+3

, endianess , , endianess.

, , .

+2

, , , , "". , , . , , . ( , long ints).
htonl(3) .

0

Memcpy just copies bytes and doesn't care about byte order. Therefore, if you want to copy one network stream to another, use memcpy.

0
source

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


All Articles