Article View: comp.lang.c.moderated
Article #2359Re: Freeing Memory
From: Kurt Watzka
Date: Sat, 23 Mar 1996 06:44
Date: Sat, 23 Mar 1996 06:44
108 lines
3603 bytes
3603 bytes
Daniel L Mills <dmills@eng.umd.edu> writes:
>Hello all,
>I'm having problems freeing a 3-D malloced array. I thought I was doing
>it right but when I examine that status of the program's execution I
>notice that each time I reinitialize th array, the old memory does not
>get released.
Since the code you posted cannot be compiled with a C compiler,
it is hard to tell what you _really_ do.
>I'm programming on a Sun Sparc 10 using the gcc compiler.
Try using "-ansi -pedantic -Wall" with that compiler.
>My program
>reads in a large set of 3-D data into an array. Subsequent calls to the
>program require it to free the old memory, reallocate new memory, then
>read in new data. If I run the UNIX utility "top" and watch how much
>memory the program uses, I never gets smaller, only larger.
This is the nature of a lot of allocation schemes, esp. on UNIX
operating systems.
After some changes that make your program _compilable_, there is
no obvious reason why it should not free all the memory allocated to
it.
>Here's the code.
>Variables and structures:
>typedef struct Node {
> int ID;
> float power;
>};
This does not define _any_ type in C. In a similar language, the typedef
is not neccessary, because a struct declaration introduces the struct
tag as a synonym for the struct in that language.
Hereinafter, I will assume that this was spelled as
typedef struct Node {
int ID;
float power;
} Node;
>Node ***nodedata=NULL;
>int xnodes,ynodes,znodes;
>Here's how I allocate the array:
>nodedata = (Node ***)malloc(sizeof(Node **) * xnodes);
>for(i = 0;i < xnodes;i++){
> nodedata[i] = (Node **)malloc(sizeof(Node *) * ynodes);
> for(j = 0;j < ynodes;j++)
> nodedata[i][j] = (Node *)malloc(sizeof(Node) * znodes);
>}
>Here's how I free it:
>free3DNode(&nodearray,xnodes,ynodes);
"nodearray" has not been introduced to us, so I will assume that
it is just a synonym for "nodedata"!
>void free3DNode(Node ****nodearray,int icount,int jcount)
>{
> int i,j;
> if(*nodearray){
> for(i=0;i < icount; i++){
> for(j=0; j < jcount; j++)
> if((*nodearray)[i][j])
> free((*nodearray[i][j]);
This is obviously _not_ how you free it, because the number of parentheses
does not match in the previous statement.
I will assume that you really do
free((*nodearray)[i][j]);
> if((*nodearray)[i])
> free((*nodearray)[i]);
> }
> free(*nodearray);
> *nodearray = NULL;
> }
>}
A general remark about your technique of "passing a pointer to the
variable that should be freed to a deallocation function:
It is rarely worth the effort because it sets just _one_ variable
to NULL. The problem is _not_ that you reuse a pointer that has
been free using the pointer variable that was freed in most cases.
If you have _other_ references to the same memory area, you will
still have a problem, because nobody sets them to NULL.
Kurt
--
| Kurt Watzka Phone : +49-89-2180-6254
| watzka@stat.uni-muenchen.de
| ua302aa@sunmail.lrz-muenchen.de
Technical Details
Message-ID:
<4j06kh$7t5@solutions.solon.com>Path:
arc1-ca.pugleaf.net!solon.com!not-for-mailReferences:
<4io1ks$nr1@solutions.solon.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail From: watzka@stat.uni-muenchen.de (Kurt Watzka) Newsgroups: comp.lang.c.moderated Subject: Re: Freeing Memory Date: 23 Mar 1996 00:44:33 -0600 Organization: Leibniz-Rechenzentrum, Muenchen (Germany) Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4j06kh$7t5@solutions.solon.com> References: <4io1ks$nr1@solutions.solon.com> NNTP-Posting-Host: solutions.solon.com