[12] in Pthreads mailing list archive

home help back first fref pref prev next nref lref last post

Re: netdb interfaces

daemon@ATHENA.MIT.EDU (Ken Raeburn)
Sat Jun 10 20:43:40 1995

From: Ken Raeburn <raeburn@cygnus.com>
Date: Sat, 10 Jun 1995 18:27:07 -0400
To: Rich Salz <rsalz@osf.org>
Cc: pthreads@MIT.EDU
In-Reply-To: Rich Salz's message of Sat, 10 Jun 1995 02:57:55 -0400 <9506100657.AA04768@sulphur.osf.org>


   From: Rich Salz <rsalz@osf.org>
   Date: Sat, 10 Jun 1995 02:57:55 -0400

   >I don't think any object can ever require alignment greater than its
   >own size.

   You gotta be careful not to confuse alignment and padding.  For example:
	   struct x { short s; char c; };

   I can't find anything in the standard that limits the alignment
   requirements an object can have.

As Greg said, think about array allocations.  In this example, struct x,
s, and c each can require alignment no greater than their size.  In the
case of struct x, it may be less than its size, but not necessarily.

   >How about implementing it as a structure with a variable-length tail?

   Don't do this; it's gross and ugly and nasty and (quoting Ritchie)
   "unwarranted chumminess with the compiler."  But it works -- DCE RPC
   is rife with that construct, and it works.  But I hate it.

But does it violate the standard?  Yes, I know accessing outside the
array bounds is undefined in otherwise normal cases, but is allocating
the extra storage sufficient to make it always have to work through
other constraints in the standard?

   I don't understand the problem -- what are you trying to do?
   Does this solve it, perhaps at the cost of wasting a few bytes?

       #define SZ sizeof (struct servent) / sizeof (char*)
       union u {
	   struct servent s;
	   char *pad[SZ];
       };
       x = malloc(sizeof (union u) + (1+n_pointers/SZ)*sizeof (union u));
       x->s.aliases = &x[1];

You'd have to add the character buffer, and turn it around to test the
length of the supplied buffer to see if it was big enough, but otherwise
that seems ... well, about as questionable as my variable-length-tail
suggestion.  What if n_pointers is greater than SZ?  Is it valid to
access x->s.aliases[SZ]?  Is that any less gross than implementing a
variable-length tail?  Why?

home help back first fref pref prev next nref lref last post