get size of malloc'd object

Tanner Andrews tanner at ki4pv.UUCP
Wed Jun 25 00:21:15 AEST 1986


The problem with alignments, it would appear to me, would be easily
solved by allowing for the largest machine type, which should be
double.  Thus (cast the routine type as needed when you call it):

 /*  allocate core -- must free using my_free()
  */
char *my_malloc(sz)
short int sz;
	{
	register char
		*p;

	if  (p = malloc(sz+sizeof(double)))  {
		*((short int *)p) = sz;
		p += sizeof(double);
		}
	return(p);
	}

 /*  size of allocated object -- works for any p such that p is return
  *  value from my_malloc
  *  which has not been my_free'd
  */
short int my_sizeof(p)
char *p;
	{

	return(*((short int *)(p - sizeof(double)))

	}

-- 
<std dsclm, copies upon request>	   Tanner Andrews



More information about the Comp.unix mailing list