🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Freeing Memory

#2365
From: Your Name
Date: Sat, 23 Mar 1996 06:48
82 lines
3049 bytes
In article <4io1ks$nr1@solutions.solon.com>, dmills@eng.umd.edu says...
 >
 >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
 >----------------------------------------------------------------------

I've never seen anything like that before!  Assuming all you need is
a 3D array, why not:

nodedata = (Node *) malloc(sizeof(Node) * x_dim * y_dim * z_dim);

then refer to each element as nodedata[i][j][k]?

To free use:

free(nodedata);

Sorry I can't address your problem directly--all those levels of
indirection give me a headache!

Also, I think I've heard that once the OS has allocated the memory,
it stays allocated to the process even though the program has
freed it.  The memory is, of course, available for future allocation.
Technical Details
Message-ID: <4j06s3$83j@solutions.solon.com>
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
References: <4io1ks$nr1@solutions.solon.com>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail
From: Yourname@somwhere.COM (Your Name)
Newsgroups: comp.lang.c.moderated
Subject: Re: Freeing Memory
Date: 23 Mar 1996 00:48:35 -0600
Organization: Somewhere
Sender: clc@solutions.solon.com
Approved: clc@solutions.solon.com
Message-ID: <4j06s3$83j@solutions.solon.com>
References: <4io1ks$nr1@solutions.solon.com>
NNTP-Posting-Host: solutions.solon.com
X-Authentication-Warning: The author was not authenticated.
X-Disclaimer: This is the author's opinion and not that of Raytheon Company.
X-Newsreader: WinVN 0.93.11