structure alignment question

Guy Harris guy at sun.uucp
Wed Sep 24 04:01:19 AEST 1986


> What I meant was...

Then why the hell didn't you *say* that?  What you *did* say was

	"...I could use do word operations in my strnlen, strncmp,
	strncpy, and whatever other string processing functions I
	happened to write."

which sure sounds to *me* like you were doing word operations in the
standard string operations.  If you weren't referring to them, you shouldn't
have called them "my strnlen, ..."; you should have called them "my string
processing functions", and perhaps emphasized that they were *not*
replacements for the standard ones.

> Also, "fcmp" depends on the fact that a word-aligned string is
> terminated by a null word

As do the string copy and string length routines, unless you're individually
checking each byte of each word that you move.

> The point is, a word aligned string does not seem to be too much
> to ask for,

Yes, it is, considering you can do it yourself.  Just declare all your
strings to be unions of arrays of "char"s and a "short".  If you want to
initialize the string, you can probably get away with declaring it to be an
integral number of words long - if you include the extra padding word, the
compiler will even do you the favor of making that word all-zero.  If you
use ANSI C, you can even initialize the union by declaring the string to be
its first member.

Remember, ANSI C does *not* guarantee, in any way, that strings are aligned
on any boundary more strict than a byte boundary.  As such, you're going to
have to do this sort of thing anyway, unless you "know" how the compiler
works.  Yes, it means you have to type more, but that's life.

> "A little knowledge is a dangerous thing"... programmers should
> *never* learn about machine architectures: they will only want to
> write fast code...

Could you please point out where *anybody* stated their position this way?
Don't just assume that this was somebody's motivation; one of the biggest
source of errors in this business seems to be people assuming they "know"
why something was done, or how it works internally, or....

Sometimes that kind of tweaking is a good idea, and sometimes it just makes
work for you or, more likely, the guy who inherited your code when it comes
time to port it to a new architecture.  I'm tempted, at times, to use Barney
Frank's phrase and call it the programming equivalent of "crack"; it gives a
short-term high, but has bad long-term consequences.  If you're going to do
that sort of thing, 1) make sure you *really* need it, and can't get the
same performance improvement using more portable code, 2) wrap it in #ifdefs
so that people know it's there (and don't have to go through too much
trouble to compile a version for a particular machine), and 3) provide a
portable version so people can at least get it to work on a new machine
fairly quickly.

> I hope that this time I have clarified my position: everybody can
> now say I'm wrong (of course I am) or right.

You're wrong.  You don't need to have the compiler put strings on word
boundaries in order to be able to put your specialized kind of string on
word boundaries.
-- 
	Guy Harris
	{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
	guy at sun.com (or guy at sun.arpa)



More information about the Comp.lang.c mailing list