Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #142765

Re: memcpy or assign data to uint64 on little endian(linux) system

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: memcpy or assign data to uint64 on little endian(linux) system
Date 2019-04-03 11:07 +0100
Organization A noiseless patient Spider
Message-ID <877ecbl7uz.fsf@bsb.me.uk> (permalink)
References <3b06fd96-348b-423c-af57-e44e1f79b40c@googlegroups.com> <e7i8ae1tc2d5n01e6gsgahck4dinccsh1j@4ax.com> <slrn3t4oqa8jpn.j6n.ike@iceland.freeshell.org>

Show all headers | View raw


Ike Naar <ike@iceland.freeshell.org> writes:

> On 2019-04-03, Geoff <geoff@invalid.invalid> wrote:
>> On Tue, 2 Apr 2019 18:14:31 -0700 (PDT), WalkeR Park
>>>=================================================
>>>        uint64 a;
>>>        char b[8] = {0x38, 0x12, 0x12, 0x12, 0x81, 0x28, 0x28, 0x28};
>>>        memcpy(&a, b, 8);
>>>        printf("TEST [%lu]\n", a);
>>>=================================================
<cut>
>> Consider writing a reversing memmove (r_memmove) then 
>> #define memcpy r_memmove
>> for the sections where this is needed.
>>
>> Prefer memmove over memcpy.
>
> Or consider initializing a as
>
>   uint64_t a = 0x3812121281282828;
>
> This should behave the same on big-endian and little-endian machines.

That gives the same numerical value, but you can't just tell by looking
at a particular memcpy line, if the intent is numerical or not.  For
example, if you are setting up a struct representing a network packet,
it's the memcpy that will set everything correctly regardless of
endianness, whereas assigning numerical values will go wrong.  You won't
need much context to decide, but the copy line itself probably won't be
enough.

It looks like a thorough and careful isolation of all the parts the assume
byte order is needed.

-- 
Ben.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

memcpy or assign data to uint64 on little endian(linux) system WalkeR Park <walker.ykpark@gmail.com> - 2019-04-02 18:14 -0700
  Re: memcpy or assign data to uint64 on little endian(linux) system Geoff <geoff@invalid.invalid> - 2019-04-02 22:49 -0700
    Re: memcpy or assign data to uint64 on little endian(linux) system Ike Naar <ike@iceland.freeshell.org> - 2019-04-03 06:14 +0000
      Re: memcpy or assign data to uint64 on little endian(linux) system Geoff <geoff@invalid.invalid> - 2019-04-02 23:22 -0700
      Re: memcpy or assign data to uint64 on little endian(linux) system Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-04-03 11:07 +0100
        Re: memcpy or assign data to uint64 on little endian(linux) system Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-04-03 12:56 +0000
          Re: memcpy or assign data to uint64 on little endian(linux) system fir <profesor.fir@gmail.com> - 2019-04-03 06:52 -0700
          Re: memcpy or assign data to uint64 on little endian(linux) system Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-04-03 16:06 +0100
  Re: memcpy or assign data to uint64 on little endian(linux) system fir <profesor.fir@gmail.com> - 2019-04-03 04:03 -0700
    Re: memcpy or assign data to uint64 on little endian(linux) system fir <profesor.fir@gmail.com> - 2019-04-03 04:13 -0700
  Re: memcpy or assign data to uint64 on little endian(linux) system Jorgen Grahn <grahn+nntp@snipabacken.se> - 2019-04-03 12:37 +0000
  Re: memcpy or assign data to uint64 on little endian(linux) system Ike Naar <ike@iceland.freeshell.org> - 2019-04-03 16:10 +0000
    Re: memcpy or assign data to uint64 on little endian(linux) system Keith Thompson <kst-u@mib.org> - 2019-04-03 12:50 -0700

csiph-web