[net.lang.c] question on dyn. mem. alloc

fnf@unisoft.UUCP (07/23/85)

In article <1035@homxa.UUCP> delbene@homxa.UUCP (K.DELBENE) writes:
>				...	It seems that no
>matter how I declare the function calloc() or how I typecast in
>the returned pointer, I get a warning from lint that I have
>a "possible pointer alignment problem."	...

The problem here is that lint does not know anything about the
fact that malloc is guaranteed to return a pointer to space suitably
aligned for storage of any type of object (don't tell it to read
the manual :-).

Malloc is declared as returning a pointer to a character, and lint
takes this declaration at face value.  Thus lint believes that malloc
may return ANY value (on most machines) such as 0x1000, 0x1001, 0x1002, 
etc.  Casting some of these to other pointer types can be troublesome
on some machines, hence the warning.

The easiest workaround is to define your own malloc and call it
when you need storage.  Declaring it as "long *" is usually sufficient
but still not 100% portable. This limits the lint messages to exactly one.

	long *mymalloc (size)
	unsigned size;
	{
		return ((long *) malloc (size));
	}

	main ()
	{
		struct foo *foopointer;

		foopointer = (struct foo *) mymalloc (sizeof (struct foo));
	}

Hope this helps.
-Fred

===========================================================================
Fred Fish    UniSoft Systems Inc, 739 Allston Way, Berkeley, CA  94710  USA
{ucbvax,decvax}!unisoft!fnf	(415) 644 1230 		TWX 11 910 366-2145
===========================================================================