[comp.lang.c] lint warning messages with malloc

jeff@cjsa.WA.COM (Jeffery Small) (11/03/88)

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

daveh@marob.MASA.COM (Dave Hammond) (11/03/88)

In article <142@cjsa.WA.COM> jeff@cjsa.WA.COM (Jeffery Small) writes:
>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
>[...sample code deleted...]

The program code is not at fault.  The problem is that lint does not
know about malloc returning a suitably aligned pointer, therefore
it complains.  You could do some fancy declaration footwork and declare

X *malloc();

for the file, but be careful that there are no instances of any other
malloc casts (including char *) within the same file.

Most folks just ignore this warning when issued for malloc.

Dave Hammond
  UUCP: ...!uunet!masa.com!{marob,dsix2}!daveh
DOMAIN: daveh@marob.masa.com
----------------------------------------------------------------------------

crossgl@ingr.UUCP (Gordon Cross) (11/03/88)

In article <142@cjsa.WA.COM>, jeff@cjsa.WA.COM (Jeffery Small) writes:
> 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

The warning that lint is giving you is based on the fact that you are casting
a character pointer (returned from malloc) into another pointer type having
more stringent alignment requirements.  Malloc does guarantee that all 
pointers it returns are properly aligned to use with any data type but lint
does not assume this since "malloc" is just another function (you could write
your own version you know).  Your best bet is to just ignore the warning
since you are NOT in reality going to have any problems from this!


Gordon Cross
Intergraph Corp.  Huntsville, AL

guy@auspex.UUCP (Guy Harris) (11/04/88)

>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.

Incorrect assumption.  "lint" doesn't know "malloc()" from a hole in the
ground; it has no idea that it returns maximally-aligned pointers.  This
might be considered a deficiency in "lint".

There is nothing wrong with the alignment of the "*z pointer".  Do not
adjust your set.

As one version of the "lint" manual page said, under the BUGS section,
"There are some things you just *can't* get 'lint' to shut up about."

steve@umigw.MIAMI.EDU (steve emmerson) (11/04/88)

I sometimes use:

    #ifdef lint
    #   define  malloc(s)   0
    #else
        extern char *malloc();
    #endif

Unfortunately, this means that lint(1) can no longer check the type of the
argument.  C'est la vie.
-- 
Steve Emmerson                     Inet: steve@umigw.miami.edu [128.116.10.1]
SPAN: miami::emmerson (host 3074::)      emmerson%miami.span@star.stanford.edu
UUCP: ...!ncar!umigw!steve               emmerson%miami.span@vlsi.jpl.nasa.gov
"Computers are like God in the Old Testament: lots of rules and no mercy"

tonyw@microsoft.UUCP (Tony Williams) (11/05/88)

In article <355@marob.MASA.COM> daveh@marob.masa.com (Dave Hammond) writes:
|In article <142@cjsa.WA.COM> jeff@cjsa.WA.COM (Jeffery Small) writes:
|>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
|>[...sample code deleted...]
|
|The program code is not at fault.  The problem is that lint does not
|know about malloc returning a suitably aligned pointer, therefore
|it complains.  You could do some fancy declaration footwork and declare
|
|X *malloc();

DO NOT DO THIS.  Since you are using lint, I assume you care somewhat
about portability :-).  There are machines out there for which
struct X * and char * have different representations.
On these machines, malloc will return the char* representation and you
must have the cast to convert it.

For the same reason, you must have a declaration
extern char* malloc();
in your programs, so that the compiler does not assume it is int.

I am afraid that we just have to live with some features of lint.
  Tony Williams

bill@twwells.uucp (T. William Wells) (11/07/88)

In article <182@umigw.MIAMI.EDU> steve@umigw.miami.edu (steve emmerson) writes:
: I sometimes use:
:
:     #ifdef lint
:     #   define  malloc(s)   0
:     #else
:         extern char *malloc();
:     #endif
:
: Unfortunately, this means that lint(1) can no longer check the type of the
: argument.  C'est la vie.

But you could do this:

#ifdef lint
static void malloc_kludge(n) size_t n; { n = n; }
#define malloc(x) (malloc_kludge(x), 0)
#else
extern char *malloc();
#endif

---
Bill
{uunet|novavax}!proxftl!twwells!bill

meissner@xyzzy.UUCP (Usenet Administration) (11/07/88)

In article <355@marob.MASA.COM> daveh@marob.masa.com (Dave Hammond) writes:
	/* stuff about lint giving errors calling malloc deleted */
| The program code is not at fault.  The problem is that lint does not
| know about malloc returning a suitably aligned pointer, therefore
| it complains.  You could do some fancy declaration footwork and declare
| 
| X *malloc();

No, No, No, No, No, No!  There are implementations of C on word-based
machines, which use different representations for different types of
pointers.  Since the library routine malloc returns a char *, and you have
declared to return X *, you will get undefined behvior.  This is NOT
guaranteed by K&R, H&S, or X3J11.
-- 
Michael Meissner, Data General.

Uucp:	...!mcnc!rti!xyzzy!meissner
Arpa:	meissner@dg-rtp.DG.COM   (or) meissner%dg-rtp.DG.COM@relay.cs.net

daveh@marob.MASA.COM (Dave Hammond) (11/07/88)

In article <1169@microsoft.UUCP> tonyw@microsoft.UUCP (Tony Williams) writes:
>In article <355@marob.MASA.COM> daveh@marob.masa.com (Dave Hammond) writes:
>|               You could do some fancy declaration footwork and declare
>|
>|X *malloc();
>
>DO NOT DO THIS.  Since you are using lint, I assume you care somewhat
>about portability :-).  There are machines out there for which
>struct X * and char * have different representations.
>On these machines, malloc will return the char* representation and you
>must have the cast to convert it.

Sorry!  My example should have shown conditional inclusion for lint only, e.g.:

#ifdef lint
X *malloc();
#else
char *malloc();
#endif

Dave Hammond
  UUCP: ...!uunet!masa.com!{marob,dsix2}!daveh
DOMAIN: daveh@marob.masa.com
----------------------------------------------------------------------------