Article View: net.lang.c
Article #9571C structs & A question about octet
From: Lawrence W. McVoy
Date: Tue, 04 Nov 1986 23:16
Date: Tue, 04 Nov 1986 23:16
51 lines
2693 bytes
2693 bytes
I have a question about alignment and padding. I have noticed (context:
Vax 780, 4.3BSD) that the c compiler pads out struct sizes to be
long word aligned. And it does the pointer arithmetic based on the
padded sizes. (no sh*t, sherlock, one would hope that they are the same)
For instance,
typedef struct {
char byte;
short word;
} three_bytes;
sizeof(three_bytes) == 4, not 3.
three_bytes* p = 100;
p == 100, p+1 == 104, not 103.
For all of you that knew this, you're all saying big deal, so what? Well,
I do (did) stuff like this all the time:
head = (three_bytes*)calloc(N, sizeof(three_bytes));
This wastes N bytes. Sometimes N is around 10 to the 7th or 8th. Bad news.
The fact that pointer arith is "wrong" makes this very icky to work around
even if you are aware of the problem. Anyone have any comments or
suggestions? Does everyone except me know about this?
Also, what's this about alignment that I hear all the time? If compilers are
already aligning things for you, why bother to do it explicitly? You might
say "so it works on stupid compilers" but who are you to say what alignment
should be? I mean, if you port code to a machine with 24 bit alignment and
you've carefully aligned all your stuff to 32 bit boundries, you've screwed
yourself. No fun. Also no gain.
OK, next question: I want to define some types to hold bytes, words, and
long words, where byte == 8 undsigned bits, word == 16 unsigned bits, and
long words == 32 unsigned bits. I want to give them nice names, names
that imply the number of bits. I could use u8, u16, and u32, but I
don't *like* those names. I thought I had a better plan:
use octet for the byte
use hexdectet for sixteen
use <latin for two>, latin for 30> for 32
but 32 turned out to be "duotrentet" or something and that's ugly. So
does anyone have any better names? Something nice and intuitive and
not ugly? How about Greek? How do they spell them?
--
Larry McVoy mcvoy@rsch.wisc.edu,
{seismo, topaz, harvard, ihnp4, etc}!uwvax!mcvoy
"They're coming soon! Quad-stated guru-gates!"
Technical Details
Message-ID:
<2904@rsch.WISC.EDU>Path:
arc1-ca.pugleaf.net!utzoo!watmath!clyde!rutgers!uwvax!mcvoyShow Full Headers
Complete Article Headers
Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: arc1-ca.pugleaf.net!utzoo!watmath!clyde!rutgers!uwvax!mcvoy From: mcvoy@rsch.WISC.EDU (Lawrence W. McVoy) Newsgroups: net.lang.c,comp.lang.c Subject: C structs & A question about octet Message-ID: <2904@rsch.WISC.EDU> Date: Tue, 4-Nov-86 18:16:27 EST Article-I.D.: rsch.2904 Posted: Tue Nov 4 18:16:27 1986 Date-Received: Wed, 5-Nov-86 05:20:02 EST Distribution: net Organization: U of Wisconsin CS Dept Lines: 51 Keywords: Alignment, structs, word sizes