freeing dynamic memory in the r.le programs

I have used the following code to allocate and free memory in one of the
r.le programs:

  pt = (int *)G_calloc(100, sizeof(int));

        ...

  for (i=0; i<100; i++)
     free(pt[i]);
  free(pt);

This works fine on Sun systems with a version of Unix System V, but
failsproduces the compiler message "incompatible integer/pointer combination"
on Apollo machines with bsd4.3. I forgot to mention that the declaration
is "int *pt;" before the above code. This problem was found by Lars
Schylberg.

Lars was able to fix the problem on the Apollo by replacing free(pt[i])
with free(&pt[i]). However, when I do this on my Sun I quickly run out
of memory, indicating that the memory is not being freed correctly.

I am simply a beginning programmer and can't find anything in the usual
C books about this. Anyone know a universal method for freeing memory
that will work with System V and bsd?

  Bill Baker
  bakerwl@corral.uwyo.edu
  Univ. of Wyoming, Laramie
  307-766-3311

BAKERWL (BAKERWL@corral.uwyo.edu) writes on 18 Jul 93:

I have used the following code to allocate and free memory in one of the
r.le programs:
pt = (int *)G_calloc(100, sizeof(int));

for (i=0; i<100; i++)
   free(pt[i]);
free(pt);

is "int *pt;" before the above code. This problem was found by Lars

My understanding of C is that you only need one call to
free for every malloc call. Therefore,
        free(pt);
should be sufficient.

When higher dimension arrays are used, then you have to
call free more than once. For example, for an NxN array:

  int **pt;
  
  if ( (pt = (int **) malloc(N*sizeof(int *))) == NULL)
    fprintf(stderr,"opps\n"), exit(-1);
  for (i=0;i<N;++i)
    if ( (pt[i] = (int *) malloc(N*sizeof(int))) == NULL)
      fprintf(stderr,"opps\n"), exit(-1);

  ...

  for (i=0;i<N;++i)
    free(pt[i]);
  free(pt);

see the FAQ for comp.lang.c (available via anonymous ftp
from rtfm.mit.edu:pub/usenet/comp.lang.c).

BTW, is there a nice way to run lint with the Gmakefile
setup? I currently do something like this:

CC = lint
COMPILE_FLAGS =

--Darrell