Xref: utzoo comp.lang.c:22613 comp.unix.wizards:18575 Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!mailrus!wuarchive!ukma!sean From: sean@ms.uky.edu (Sean Casey) Newsgroups: comp.lang.c,comp.unix.wizards Subject: How do I keep pointers aligned? Message-ID: <12879@s.ms.uky.edu> Date: 9 Oct 89 18:22:50 GMT Organization: The Leaning Tower of Patterson Office @ The Univ. of KY Lines: 56 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@ms.uky.edu, sean@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?''