How do I keep pointers aligned?

Sean Casey sean at ms.uky.edu
Tue Oct 10 04:22:50 AEST 1989


Lint tells me I have possible pointer alignment problems.

In one case, I'm allocating storage for a struct:

typedef struct Stuff {
	int stufflet;
	etc;
} STUFF;

s = (STUFF *) malloc((unsigned) sizeof(STUFF));

Now we all know malloc returns a (char *). What I'm wondering is why
lint would complain. Aren't all pointers the same size? Does lint think
that perhaps a four byte pointer might be copied to a two byte pointer
that is not aligned so that it can be interpreted as a 4BP?

Am I missing something here?

In another case, when using varargs and given the definitions:

(in /usr/include/varargs.h)
# define va_arg(list,mode) ((mode *)(list += sizeof(mode)))[-1]

char *strings[255];

while (strings[argno++] = va_arg(ap, char *));

Produces the same warning, even though it exactly matches the usage example
given in the manual page. It does work, by the way.

Can I assume that a compiler will take:

struct Bunga {
	blah;
	any number of weird blah;
	struct Bunga *next;
};

And insure that next is properly aligned? Can I assume malloc always returns
me a char * that is aligned so that the above always works?

I'm using this stuff in a teleconferencing system server that's been
running for several months now. As far as I can tell, there's only one
instance where pointer alignment may have actually caused a crash, and
I'm not really sure about that.

I'm curious to know if i need to worry about this stuff. If not, I'd like
to know why lint complains.

Thanks,

Sean
-- 
***  Sean Casey          sean at ms.uky.edu, sean at ukma.bitnet, ukma!sean
***  Copyright 1989 by Sean Casey. Only non-profit redistribution permitted.
***  ``So if you weight long enough, you'll get your packets, right?''



More information about the Comp.unix.wizards mailing list