3 ms·
While it is true that compilers pad for alignment it is pretty easy to avoid that, at least in my experience. So an easier approach, but more fragile, is just
by luckydude 11y ago
While it is true that compilers pad for alignment it is pretty easy to avoid that, at least in my experience.
So an easier approach, but more fragile, is just to define the struct, count up how big you think it
should be and then do an
assert(sizeof(struct foo) == FOO_SIZE);
If that assert doesn't pop then you can just do
write(fd, &foo, FOO_SIZE);
I know that is a lot less elegant but it works and isn't doing the translation back and forth.
If you don't like that you might consider using Sun's RPC marshalling code that is (used to be?)
part of Linux.
- luckydude 11y agoYeah, in poking around it looks like RPC is still a thing and it's solved this problem in an automated way. http://docs.freebsd.org/44doc/psd/23.rpc/paper.pdf http://docs.freebsd.org/44doc/psd/23.rpc/paper.pdf http://docs.freebsd.org/44doc/psd/22.rpcgen/paper.pdf http://docs.freebsd.org/44doc/psd/22.rpcgen/paper.pdf