Re: is portable aliasing possible in C++?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



// on a big-endian 64-bit machine

struct Message {
    int32_t a;
    int16_t b;
    char c;
    char padding;
};

// send over a socket
Message msg = {12345, 678, 'x', 0};
send(fd, &msg, sizeof Message, 0);


// another machine: a little-endian 64-bit machine
char buffer[1024];
if (recv(fd, buffer, sizeof buffer, 0)) {
    Message msg;
    // or we can use the union trick
    memcpy(&msg, buffer, sizeof Message);    SomeFunctioToConvertFromBigEndianToSmallEndian(msg.a);

    SomeFunctioToConvertFromBigEndianToSmallEndian(msg.b);
}



On Monday, September 15, 2014 7:33 PM, Andrew Haley <aph@xxxxxxxxxx> wrote:
On 09/15/2014 12:29 PM, Hei Chan wrote:
> 
> 
> 
> 
> 
> On Monday, September 15, 2014 7:22 PM, Andrew Haley <aph@xxxxxxxxxx> wrote:
> 
>> Using memcpy(), the compiler will have to make a copy
>> because it sees that few lines, for example, down, the program tries
>> to manipulate the copy.
> 
> So, don't manipulate the copy, then.  Use it once, then throw it away.
> 
> Sometimes, due to the endianness, I am forced to manipulate the copy...

I don't know what you mean.  A small example would help.




Andrew.




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux