From: Andrew Haley <aph@redhat.com>
To: Hei Chan <structurechart@yahoo.com>,
"gcc-help@gcc.gnu.org" <gcc-help@gcc.gnu.org>
Subject: Re: is portable aliasing possible in C++?
Date: Mon, 15 Sep 2014 14:11:00 -0000 [thread overview]
Message-ID: <5416F397.1010500@redhat.com> (raw)
In-Reply-To: <1410787872.60310.YahooMailNeo@web165006.mail.bf1.yahoo.com>
On 09/15/2014 02:31 PM, Hei Chan wrote:
>
>
> On Monday, September 15, 2014 9:21 PM, Andrew Haley <aph@redhat.com> wrote:
> On 09/15/2014 12:57 PM, Hei Chan wrote:
>> // 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};
>
> I'd do this:
>
> Message msg = {htonl(12345), htons(678), 'x', 0};
>
> What if I have no control of the sender?
I don't understand this question. You said that you were sending.
>> 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
>
> Why not read into the Message?
>
> I think it goes back to the same issue as Andy (the original poster)
> -- the server/sender will send multiple kinds of message types.
>
> For instances,
>
> enum class MsgType : int32_t {
> Heartbeat,
> Logon,
> Logout
> };
>
> struct Header {
> int32_t MsgType;
> int32_t padding;
> }
>
> struct LogonBody {
> int32_t a;
> int16_t b;
> char c;
> char padding;
> }
>
> I can read into an instance of Header first (1 system call), then I
> know the message type so I can read into an instance of LogonBody
> (another system call). But my goal is to avoid latency. I
> certainly would prefer 1 system call instead of 2 if possible.
But you can either read into a union of the message types and handle
that, or read into a byte array and pick the data straight from there.
Like this:
int
byteswap (int u)
{
return ((((u) & 0xff000000) >> 24)
| (((u) & 0x00ff0000) >> 8)
| (((u) & 0x0000ff00) << 8)
| (((u) & 0x000000ff) << 24));
}
int readInt(char *a) {
int val;
memcpy(&val, a, sizeof val);
return byteswap(val);
}
which generates this:
readInt:
ldr w0, [x0]
rev w0, w0
ret
> Hope now it makes sense to you.
Not really, no. I know that there are many bad ways of solving the
problem. All I'm saying is that you don't have to do it in a bad way.
C provides you with everything you need to do it well.
If you want a way to read from an arbitrary position in an byte array
into any type, in any endianness, you can do that; see readInt above.
If you want to read into a union of all message types, you can do
that.
Andrew.
next prev parent reply other threads:[~2014-09-15 14:11 UTC|newest]
Thread overview: 35+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-09-10 23:03 haynberg
2014-09-11 8:11 ` Andrew Haley
2014-09-11 23:25 ` haynberg
2014-09-12 8:32 ` Andrew Haley
2014-09-12 22:58 ` haynberg
2014-09-13 7:23 ` Andrew Haley
2014-09-13 11:45 ` Oleg Endo
2014-09-15 2:37 ` Hei Chan
2014-09-15 8:35 ` Andrew Haley
2014-09-15 11:07 ` Hei Chan
2014-09-15 11:21 ` Andrew Haley
2014-09-15 11:29 ` Hei Chan
2014-09-15 11:32 ` Andrew Haley
2014-09-15 11:57 ` Hei Chan
2014-09-15 13:21 ` Andrew Haley
2014-09-15 13:31 ` Hei Chan
2014-09-15 14:11 ` Andrew Haley [this message]
2014-09-15 11:27 ` Jonathan Wakely
2014-09-15 12:09 ` Paul Smith
2014-11-02 23:55 ` Hei Chan
2014-11-03 9:34 ` Andrew Haley
-- strict thread matches above, loose matches on Subject: below --
2014-09-09 23:13 haynberg
2014-09-10 8:17 ` Andrew Haley
[not found] <A76FB9DDEDFA994BAF6B77704A4AF465BC2464@xchmbbal502.ds.susq.com>
2014-09-04 16:11 ` Andy Webber
2014-09-04 16:51 ` Andrew Haley
2014-09-04 17:18 ` Andy Webber
2014-09-04 17:23 ` Andrew Haley
2014-09-04 17:44 ` Andy Webber
2014-09-04 17:47 ` Andy Webber
2014-09-04 17:48 ` Andrew Haley
2014-09-04 23:11 ` Jonathan Wakely
2014-09-05 7:16 ` Andrew Haley
2014-09-05 14:19 ` Jason Merrill
2014-09-08 9:33 ` Richard Biener
2014-09-10 14:31 ` Jason Merrill
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5416F397.1010500@redhat.com \
--to=aph@redhat.com \
--cc=gcc-help@gcc.gnu.org \
--cc=structurechart@yahoo.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).