Thread View: comp.lang.c.moderated
5 messages
5 total messages
Started by Daniel L Mills
Wed, 20 Mar 1996 04:30
Freeing Memory
Author: Daniel L Mills
Date: Wed, 20 Mar 1996 04:30
Date: Wed, 20 Mar 1996 04:30
62 lines
2140 bytes
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
----------------------------------------------------------------------
Re: Freeing Memory
Author: 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
Re: Freeing Memory
Author: William Kaufman
Date: Sat, 23 Mar 1996 06:45
Date: Sat, 23 Mar 1996 06:45
35 lines
1819 bytes
1819 bytes
In article <4io1ks$nr1@solutions.solon.com> Daniel L Mills <dmills@eng.umd.edu> writes:
]
] 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.
From the comp.lang.c FAQ:
7.25: I have a program which mallocs and later frees a lot of memory,
but memory usage (as reported by ps) doesn't seem to go back
down.
A: Most implementations of malloc/free do not return freed memory
to the operating system (if there is one), but merely make it
available for future malloc() calls within the same program.
Chances are, it shouldn't grow by much, as long as the array size(s)
are consistent or decreasing.
] 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]);
] }
Note that, if ((*nodearray)[i]) is NULL, the stuff in the loop will
fail. You should move this test outside the double "for"-loop.
-- Bill K.
Bill Kaufman, | "People look better under arrest,...it gives you
wkaufman@us.oracle.com | an aggressive fashion look." -- John Waters
Re: Freeing Memory
Author: Your Name
Date: Sat, 23 Mar 1996 06:48
Date: Sat, 23 Mar 1996 06:48
82 lines
3049 bytes
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.
Re: Freeing Memory
Author: Kurt Watzka
Date: Sun, 24 Mar 1996 17:48
Date: Sun, 24 Mar 1996 17:48
49 lines
1889 bytes
1889 bytes
Yourname@somwhere.COM (Your Name) writes:
Do people who are on a first name basis with you just call you "Your"?
> >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);
> >}
> >
>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]?
Maybe because this will not work? If both "y_dim" and "z_dim" are
compile time constants, you could do something like
Node (* nodedata)[y_dim][z_dim];
nodedata = malloc(sizeof(Node) * x_dim * y_dim * z_dim);
Now you can use the notation proposed by "Your"
>To free use:
>free(nodedata);
>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.
This is true for a lot of systems I know.
Kurt
--
| Kurt Watzka Phone : +49-89-2180-6254
| watzka@stat.uni-muenchen.de
| ua302aa@sunmail.lrz-muenchen.de
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads