Path: utzoo!attcan!uunet!mcvax!ukc!stl!stc!idec!alice!fox From: fox@alice.marlow.reuters.co.uk (Paul Fox) Newsgroups: comp.unix.wizards Subject: Re: signal 10 in malloc call??? Keywords: sys V rel 3.1, died in malloc? Message-ID: <350@alice.marlow.reuters.co.uk> Date: 16 May 88 10:42:08 GMT References: <3989@killer.UUCP> <640@vsi.UUCP> <4016@killer.UUCP> <2149@quacky.mips.COM> <1620@rpp386.UUCP> Reply-To: fox@alice.UUCP (Paul Fox) Organization: Reuters Ltd PLC, Marlow, Bucks, England Lines: 79 In article <1620@rpp386.UUCP> jfh@rpp386.UUCP (The Beach Bum) writes: >In article <2149@quacky.mips.COM> dce@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 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@alice.marlow.reuters.co.uk