lint warning messages with malloc()

Jeffery Small jeff at cjsa.WA.COM
Thu Nov 3 04:56:47 AEST 1988


When compiling a program on an AT&T 3B1 (running SystemV.2 +/-), I am
getting the following warning message from lint when I use malloc(3):

	warning: possible pointer alignment problem

Here is some sample code to illustrate the problem:

------- cut here -------------------------------------------------------
#define NULL 0

typedef struct {
	unsigned int	x;
	unsigned int	y;
	unsigned char  *z;
} X;

main()
{
    X *getptr();

    if ( getptr() == (X *) NULL)
	return (1);
    else
        return(0);
    /*NOTREACHED*/
}

X *getptr()
{
    char *malloc();

    return( (X *) malloc(sizeof(X)) );   /* WARNING FOR THIS LINE */
}
------ end -----------------------------------------------------------

Now, since the man page for malloc reads:

	Malloc returns a pointer to a block of at least size bytes
	suitably aligned for any use.

I assume that lint is complaining not about the pointer returned from
malloc(), but instead, about the alignment of the *z pointer which is
a member of the structure.

The program works on this machine, and while I know that lint is warning
about a portability problem to other architectures, I do not know what I
need to do to satisfy lint.  Is some manual padding required within the
structure, and if so, what are the rules for this.

BTW, I have tried a number of different arrangements of the structure
members with no satisfactory results.

Thanks for any pointers.  (Yea, I know :-)
--
Jeffery Small    (206) 485-5596            uw-beaver!uw-nsr!uw-warp
C. Jeffery Small and Associates                                    !cjsa!jeff
19112 152nd Ave NE - Woodinville, WA  98072           uunet!nwnexus



More information about the Comp.lang.c mailing list