Newsgroups: info.grass.programmer
Path: zorro.cecer.army.mil!shapiro
From: shapiro@zorro.cecer.army.mil (Michael Shapiro)
Subject: Re: freeing dynamic memory in the r.le programs
Message-ID: <CAFKIr.7IK@news.cecer.army.mil>
Sender: news@news.cecer.army.mil (Net.Noise owner)
Organization: US Army Corps of Engineers Construction Engineering Research Labs
References: <5238311518071993_A00879_POSSE_117793CF1300*@mrgate.uwyo.edu>
Date: Mon, 19 Jul 1993 21:18:26 GMT
Lines: 42
In <5238311518071993_A00879_POSSE_117793CF1300*@mrgate.uwyo.edu> BAKERWL@corral.uwyo.edu (BAKERWL) writes:
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
produces 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.
Based on the code segment, the for loop is the incorrect thing to do. You
only allocated memory for pt, not pt[0] - pt[100] so you should only do
free(pt)
Your compiler is telling you something here. free(pt[i]) is calling
free() with an int, not a pointer.
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.
This is definitely NOT the right thing to do.
C books about this. Anyone know a universal method for freeing memory
that will work with System V and bsd?
You are doing (almost) the right things here. SYSV and BSD
use the same free() and malloc() interface.
--
Michael Shapiro shapiro@zorro.cecer.army.mil
U.S. Army CERL (217) 373-7277
P.O. Box 9005
Champaign, Ill. 61826-9005