signal 10 in malloc call???
Paul Fox
fox at alice.marlow.reuters.co.uk
Mon May 16 20:42:08 AEST 1988
In article <1620 at rpp386.UUCP> jfh at rpp386.UUCP (The Beach Bum) writes:
>In article <2149 at quacky.mips.COM> dce at mips.COM (David Elliott) writes:
>
>below is some code i use to check the consistency of mallocs in a large
>database i am working on.
Oh well, I may as well post some code ... the following is my front
end to malloc/free/realloc. I use this to ensure that I do not corrupt
my memory areas, or try to free something thats never been allocated.
This code is portable - but requires you to call chk_alloc/chk_free and
chk_realloc although #defines could be used avoid changing existing code.
If this code is used, then any memory freed by a normal free() must have
been allocated by a malloc() (not chk_alloc()). Its usually best to
recompile everything and link in the library.
If a section 3 function calls malloc() it will bypass chk_alloc, and so
the freeing of this memory must be done by free() (not chk_free()).
-----cut here------
# include <stdio.h>
extern char *malloc();
# define MAGIC 0x464f5859L /* FOXY */
# define FREED 0x46524545L /* FREE */
int cnt_alloc = 0;
char *
chk_alloc(n)
{ register char *cp = malloc(n + 4);
register long *lp = (long *) cp;
if (lp) {
*lp++ = MAGIC;
cnt_alloc++;
}
return (char *) lp;
}
char *
chk_realloc(ptr, n)
char *ptr;
{ char *realloc();
long *lp = (long *) ptr;
if (*--lp != MAGIC)
chk_failed("Realloc non-alloced memory.");
lp = (long *) realloc((char *) lp, n+4);
return (char *) (lp + 1);
}
chk_free(ptr)
char *ptr;
{ long *lp = (long *) ptr;
if (*--lp == FREED)
chk_failed("Trying to free already freed memory.");
if (*lp != MAGIC)
chk_failed("Freeing non-alloced memory.");
cnt_alloc--;
*lp = FREED;
free((char *) lp);
}
chk_failed(str)
char *str;
{
fprintf(stderr, "CHK_ALLOC: %s\r\n", str);
abort();
}
---- cut here ------
=====================
// o All opinions are my own.
(O) ( ) The powers that be ...
/ \_____( )
o \ |
/\____\__/
_/_/ _/_/ UUCP: fox at alice.marlow.reuters.co.uk
More information about the Comp.unix.wizards
mailing list