🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c.moderated
Article #2343

Freeing Memory

#2343
From: Daniel L Mills
Date: Wed, 20 Mar 1996 04:30
62 lines
2140 bytes
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.

I'm programming on a Sun Sparc 10 using the gcc 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.

Here's the code.

Variables and structures:
typedef struct Node {
	int ID;
	float power;
};

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);

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]);
        if((*nodearray)[i])
            free((*nodearray)[i]);
    }
    free(*nodearray);
    *nodearray = NULL;
  }
}

 
I would appreciate any advice.

----------------------------------------------------------------------
Daniel L Mills (Dan)
University of Maryland
College Park, MD 20742
----------------------------------------------------------------------
Technical Details
Message-ID: <4io1ks$nr1@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: Daniel L Mills <dmills@eng.umd.edu>
Newsgroups: comp.lang.c.moderated
Subject: Freeing Memory
Date: 19 Mar 1996 22:30:20 -0600
Organization: University of Maryland, College Park
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4io1ks$nr1@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com
X-Mailer: Mozilla 2.0 (X11; I; SunOS 4.1.4 sun4m)