[comp.lang.c] Turboc Malloc

sac90286@uxa.cso.uiuc.edu (09/19/89)

Hello Netland!
I'm trying to make use of some dynamic memory allocation debugging 
routines I found in Byte magazine (I don't remember exactly which
issue, though). The code does some basic integrity checks of the 
free list at each call to malloc(), realloc() or free(). It relies on
some "inside knowledge" of the way the memory allocation routines are
implemented, specifically the structure of each node in the free list
and the symbol pointing to the head of the free list. The symbol they
give as the free list head pointer, _allocp, is apparently not the name
Borland uses in their malloc implementation (at least under version 2.0).

I poked around a bit and stumbled across a symbol called __first, but when
I substituted that symbol for _allocp the memory check routines crashed
immediately. Either my guess was wrong and __first isn't the pointer I need
or the node structure given in the article is different from that used by
TC. 

Can some kind soul provide me with the structure declaration as well as
the name of the free list head pointer? I'd be most appreciative.

Scott
kubla@uiuc.edu

gordon@qfagus.OZ (Peter Gordon) (02/28/90)

>	Please tell me I'm doing something stupid.
>		head = (char **)malloc(200 * sizeof(char **));
>		for(cp = head, i = 0; i < 200; ++i, ++cp)
>		{
>			fprintf(stdout,"Freeing %d\n", i);
>			fflush(stdout);
>			free(cp);
>		}
>	}
My face is red, as suspected the fault is mine and is stupid.  I'm
freeing a pointer and THEN trying to increment it.
Code something like:
X	for(cp = &head[198]; cp >= head; --cp)
X		free(cp + 1);
X	free(head);
works as expected.  People abuse the bugs in compilers, but in many
instances, they are very forgiving of fools such as I.

Peter Gordon

darcy@druid.uucp (D'Arcy J.M. Cain) (03/01/90)

In article <26317@qfagus.OZ> gordon@qfagus.OZ (Peter Gordon) writes:
>
>>	Please tell me I'm doing something stupid.
>>		head = (char **)malloc(200 * sizeof(char **));
>>		for(cp = head, i = 0; i < 200; ++i, ++cp)
>>		{
>>			fprintf(stdout,"Freeing %d\n", i);
>>			fflush(stdout);
>>			free(cp);
>>		}
>>	}
>My face is red, as suspected the fault is mine and is stupid.  I'm
>freeing a pointer and THEN trying to increment it.
>Code something like:
>X	for(cp = &head[198]; cp >= head; --cp)
>X		free(cp + 1);
>X	free(head);
>works as expected.  People abuse the bugs in compilers, but in many
>instances, they are very forgiving of fools such as I.
>
>Peter Gordon

Sorry.  It's getting redder.  To free the memory allocated by the malloc
you simply do:
    free(head);
once.  No loop required, one free frees the entire block allocated.  What
you are doing may even work on most compilers and OS's simply because most of
the calls to free are ignored.  (They ae undefined in fact.)  This may lead
you to believe that you can free head[0] and then access head[27].  Expect
to dump core on a standard UNIX box or anything with good memory protection
if you do this.

BTW: Perhaps you should explain *exactly* what it is this code is trying
to accomplish.  With that knowledge I am sure someone can suggest the
best way of doing what you want to do.

-- 
D'Arcy J.M. Cain (darcy@druid)     |   Thank goodness we don't get all 
D'Arcy Cain Consulting             |   the government we pay for.
West Hill, Ontario, Canada         |
(416) 281-6094                     |