N
Ning
Thanks for the replies on my last post
"Must I free allocated memory before the program exits? "
I was aware the danger of memory leak, and you brought up
some excellent points that I wasn't pay much attention to.
The problem is that I find it's hard to write a program with no
memory leak. Like in my course project, I am supposed to
allocate a two dimensional array of doubles. I use a double**
as the pointer pointing to an array of double*'s. Each of the
double* is pointing to an array of doubles. The problem comes
in when I try to free() the allocated. If I had problem when I try
to allocate memory for the array that the double** was
supposed to point to, then double** is uninitialized and I
shouldn't free the double arrays that the fake double* array is
pointing to. Sorry for using "array" loosely, hope you can get
my point. My solution to this is to initialize every pointer to
NULL, so at the end I can see which pointer is never malloc'ed
or malloc() failed. Moreover, if I call free(NULL), the behavior is
not undefined. So my program looks like this:
#include <stdio.h>
#include <stdlib.h>
#define N 10
int main()
{
int i;
/* initialize pp */
double **pp = NULL;
int ret_value = 0;
/* allocate a NxN array */
pp = malloc(N * sizeof(*pp));
if ( !pp )
goto malloc_fail;
/* initialize pp[n] */
for (i=0; i<N; ++i)
pp = NULL;
for (i=0; i<N; ++i)
{
pp = malloc(N * sizeof(**pp));
if ( !pp )
goto malloc_fail;
}
{
/* use the array here */
/* hopefully pointers are not screwed up */
}
main_exit:
/* pp != NULL? */
/* If not, pp's are garbage */
/* don't free them. */
if ( pp != NULL )
for (i=0; i<N; ++i)
free(pp);
free(pp);
return (ret_value);
malloc_fail:
ret_value = 1;
fflush(NULL);
fprintf(stderr, "malloc() fails.\n");
fflush(stderr);
goto main_exit;
}
Is there any problem in my code? Is there a
somewhat standard way to handle situations like this? Initialize every
pointer to NULL is not hard to remember, but it is just so easy to
forget.
Thanks.
"Must I free allocated memory before the program exits? "
I was aware the danger of memory leak, and you brought up
some excellent points that I wasn't pay much attention to.
The problem is that I find it's hard to write a program with no
memory leak. Like in my course project, I am supposed to
allocate a two dimensional array of doubles. I use a double**
as the pointer pointing to an array of double*'s. Each of the
double* is pointing to an array of doubles. The problem comes
in when I try to free() the allocated. If I had problem when I try
to allocate memory for the array that the double** was
supposed to point to, then double** is uninitialized and I
shouldn't free the double arrays that the fake double* array is
pointing to. Sorry for using "array" loosely, hope you can get
my point. My solution to this is to initialize every pointer to
NULL, so at the end I can see which pointer is never malloc'ed
or malloc() failed. Moreover, if I call free(NULL), the behavior is
not undefined. So my program looks like this:
#include <stdio.h>
#include <stdlib.h>
#define N 10
int main()
{
int i;
/* initialize pp */
double **pp = NULL;
int ret_value = 0;
/* allocate a NxN array */
pp = malloc(N * sizeof(*pp));
if ( !pp )
goto malloc_fail;
/* initialize pp[n] */
for (i=0; i<N; ++i)
pp = NULL;
for (i=0; i<N; ++i)
{
pp = malloc(N * sizeof(**pp));
if ( !pp )
goto malloc_fail;
}
{
/* use the array here */
/* hopefully pointers are not screwed up */
}
main_exit:
/* pp != NULL? */
/* If not, pp's are garbage */
/* don't free them. */
if ( pp != NULL )
for (i=0; i<N; ++i)
free(pp);
free(pp);
return (ret_value);
malloc_fail:
ret_value = 1;
fflush(NULL);
fprintf(stderr, "malloc() fails.\n");
fflush(stderr);
goto main_exit;
}
Is there any problem in my code? Is there a
somewhat standard way to handle situations like this? Initialize every
pointer to NULL is not hard to remember, but it is just so easy to
forget.
Thanks.