Path: utzoo!attcan!uunet!lll-winken!lll-tis!helios.ee.lbl.gov!pasteur!ucbvax!decwrl!labrea!csli!gandalf From: gandalf@csli.STANFORD.EDU (Juergen Wagner) Newsgroups: comp.lang.c Subject: Re: lint on malloc calls Message-ID: <5427@csli.STANFORD.EDU> Date: 12 Sep 88 02:37:51 GMT References: <39617@linus.UUCP> <9900007@bradley> Reply-To: gandalf@csli.stanford.edu (Juergen Wagner) Organization: Center for the Study of Language and Information, Stanford U. Lines: 60 In article <9900007@bradley> brian@bradley.UUCP writes: > >> /* Written 3:12 pm Sep 8, 1988 by jgb@linus.UUCP */ ... >> entry *nextentry; >> >> nextentry = (entry *) malloc(sizeof(entry)); >> >> Running lint -bach yields: >> >> phone.c(61): warning: illegal pointer combination >> phone.c(61): warning: possible pointer alignment problem ... > > You probably didn't tell lint or cc what malloc() returns, so it >assumed int malloc()... The warning is given because you have done >an explicit cast of a pointer to an integer. Just add the following >line to either the top of your program or the top of your function: > >extern char *malloc(); That might well be. If you don't use the above declaration lint will complain. Yet, it should say that this is an "illegal combination of pointer and integer", not what you read above. > Or, if you compiler supports void* > >extern void *malloc(); > What is that supposed to do? Casting that to a (char *) I got a warning because (void *) objects may not be byte-addressable. Still, I think, all this doesn't account for one big problem: what do you do if you have a program like cp = (char *) malloc(10*sizeof(char)); sp = (short *) malloc(10*sizeof(short)); dp = (double *) malloc(10*sizeof(double)); where malloc indeed returns values of different type. Assuming (double *) fits them all isn't sufficient. This might break on a machine with different representations for word and for byte pointers. I don't think, there is any other way than to make lint happy by saying # ifdef lint extern char *malloc_char(); extern short *malloc_short(); extern double *malloc_double(); # else !lint # define malloc_char malloc # define malloc_short malloc # define malloc_double malloc # endif lint That's ugly but didn't come across any panacea for this problem. If other people have other suggestions, please let me know. -- Juergen "Gandalf" Wagner, gandalf@csli.stanford.edu Center for the Study of Language and Information (CSLI), Stanford CA