Path: utzoo!mnetor!uunet!lll-winken!lll-tis!ames!nrl-cmf!mailrus!tut.cis.ohio-state.edu!bloom-beacon!mit-eddie!killer!pollux!dalsqnt!rpp386!jfh
From: jfh@rpp386.UUCP (John F. Haugh II)
Newsgroups: comp.unix.wizards
Subject: Re: signal 10 in malloc call???
Message-ID: <1620@rpp386.UUCP>
Date: 10 May 88 01:21:32 GMT
References: <3989@killer.UUCP> <640@vsi.UUCP> <4016@killer.UUCP> <2149@quacky.mips.COM>
Reply-To: jfh@rpp386.UUCP (The Beach Bum)
Organization: Big "D" Home for Wayward Hackers
Lines: 65
Keywords: sys V rel 3.1, died in malloc?

In article <2149@quacky.mips.COM> dce@mips.COM (David Elliott) writes:
>In article <4016@killer.UUCP> toma@killer.UUCP (Tom Armistead) writes:
>>He told me to use the special malloc(3X) library with
>>'-lmalloc' on the cc command line. I did this and the problem has gone
>>away!!!  "Thank you Antie Em, I'm not CRAZY!!!"
>
>I hate to burst your bubble, Tom, but this doesn't really show that the
>standard libc malloc() is broken.

[ and later he goes to say it also doesn't prove you don't have a bug in
  your code. ]

below is some code i use to check the consistency of mallocs in a large
database i am working on.  it checks the number of malloc/free pairs, and
the leading edge for consistency.  this code is copyright john f. haugh ii,
1987, 1988, all rights reserved (by the way ;-)  [ and for the `but your
code has X problem' people - it works on everything it has been run on.
trouble is getting it to run on a 9370 and a PC/XT without major changes. ]


static	int	d_malcnt;

char	*x_malloc (size)
int	size;
{
	char	*cp;
	char	**tp;

	if (! (cp = malloc (size + sizeof (char *))))
		abort ();

	d_malcnt++;
	tp = (char **) cp;
	*tp = &cp[sizeof (char *)];
	return (*tp);
}

x_free (cp)
char	*cp;
{
	char	**tp;

	if (cp == (char *) 0)
		abort ();

	tp = (char **) &cp[- sizeof (char *)];

	if (*tp != cp)
		abort ();

	*tp = (char *) 0;
	free (tp);
	if (! d_malcnt--)
		abort ();
}

using this particular code (with the comments still present no less ;->
has helped me locate a countless number of bugs in the code.  adding code
to check for the upper edge would help some too.

- john.
-- 
John F. Haugh II                 | "You see, I want a lot. Perhaps I want every
River Parishes Programming       | -thing.  The darkness that comes with every
UUCP:   ihnp4!killer!rpp386!jfh  | infinite fall and the shivering blaze of
DOMAIN: jfh@rpp386               | every step up ..." -- Rainer Maria Rilke