home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!mcsun!Germany.EU.net!news.netmbx.de!zrz.tu-berlin.de!cs.tu-berlin.de!jutta
- From: jutta@opal.cs.tu-berlin.de (Jutta Degener)
- Subject: Re: Memory alignment of struct elements
- Message-ID: <1992Aug17.192444.928@cs.tu-berlin.de>
- Sender: news@cs.tu-berlin.de
- Organization: Techn. University of Berlin, Germany
- References: <1992Aug12.133132.10723@Princeton.EDU> <BsxCtz.598@unx.sas.com> <adam.714060134@mcrware>
- Date: Mon, 17 Aug 1992 19:24:44 GMT
- Lines: 36
-
- adam@microware.com (Adam Goldberg) says he's been forced to do things like:
- > typedef struct PARM {
- > char MemIn[4], MemIType, MemISize[2];
- > char MemOut[4],MemOType, MemOSize[2];
- > };
- >
- > Then on each usage of, say 'MemOut', I've got to:
- >
- > (char *)(p->MemOut)
- >
- > Ugly.
-
- Ugly? Wrong! (One more point for the identity of truth and beauty.)
-
- P->MemOut, when used in a value context like this, turns from an
- `array [4] of char' into the address of the array's first element,
- &array[0], a `pointer to char'. From a type system viewpoint you
- do not need the cast.
-
- What you actually want is not `p->MemOut', but `the value of the
- bytes in p->MemOut, were they the bytes of the memory representation
- of a properly aligned pointer to char'. The only portable way to get
- this is something along the lines of
-
- char * cp;
- *(char **)memcpy((char *)&cp, p->MemOut, sizeof(cp));
-
- Of course you are free to inline the memcpy. (So is the compiler.)
-
- This kind of interface could be a job for a code-generator with a
- C-like specification language, similar to those that exist for RPC.
- A generated assembler (or nonportable C) routine could encode and
- decode the routine's parameters and results and call your C function
- in between.
-
- --Jutta
-