Do I really release memory ??

A

Andrew

Hi all, with the following code snippet I am creating a 2D matrix :

.......
double **retdouble=(double **)malloc(Y_dim*(sizeof(double *)));
if(retdouble == NULL)
{
printf("Error Allocating memory ..... \n");
exit(1);
}
for (int k=0; k<Y_dim; k++)
{
retdouble[k]=(double *)malloc(X_dim*(sizeof(double)));
// Could been *(retdouble+k) ...
if(retdouble[k]==NULL)
{
printf("Error Allocating memory ... \n");
exit(1);
}
}
................


Now once the matrix is created some operations have to be perfomred
and then i am supposed to erase the memory the matrix is holding ...

When typing "free(retdouble)" is all of the memory (
(X_dim)*(Y_dim)*(double) ) released ????
Any help appreciated

(Sorry for my english )
 
K

Kevin Goodsell

Andrew said:
Hi all, with the following code snippet I am creating a 2D matrix :

......
double **retdouble=(double **)malloc(Y_dim*(sizeof(double *)));

Consider using the c.l.c-approved malloc idiom:

p = malloc(n * sizeof *p);

It's shorter and safer. Casting malloc's return is usually not
recommended. Search this group for more discussion of this topic.
if(retdouble == NULL)
{
printf("Error Allocating memory ..... \n");

Error output should go to stderr:

fprintf(stderr, "Error Allocating memory ... \n");

1 is not a portable exit value. Consider using one of the 3 values that
are endorsed by the standard: 0, EXIT_SUCCESS, or EXIT_FAILURE.
}
for (int k=0; k<Y_dim; k++)
{
retdouble[k]=(double *)malloc(X_dim*(sizeof(double)));

retdouble[k] = malloc(X_dim * sizeof *retdouble[k]);
// Could been *(retdouble+k) ...
if(retdouble[k]==NULL)
{
printf("Error Allocating memory ... \n");
exit(1);
}
}
...............


Now once the matrix is created some operations have to be perfomred
and then i am supposed to erase the memory the matrix is holding ...

When typing "free(retdouble)" is all of the memory (
(X_dim)*(Y_dim)*(double) ) released ????

No. You need a free() call for each malloc() call.

-Kevin
 
A

Andrew

Andrew said:
Hi all, with the following code snippet I am creating a 2D matrix :

......
double **retdouble=(double **)malloc(Y_dim*(sizeof(double *)));

Consider using the c.l.c-approved malloc idiom:

p = malloc(n * sizeof *p);

It's shorter and safer. Casting malloc's return is usually not
recommended. Search this group for more discussion of this topic.
if(retdouble == NULL)
{
printf("Error Allocating memory ..... \n");

Error output should go to stderr:

fprintf(stderr, "Error Allocating memory ... \n");

1 is not a portable exit value. Consider using one of the 3 values that
are endorsed by the standard: 0, EXIT_SUCCESS, or EXIT_FAILURE.
}
for (int k=0; k<Y_dim; k++)
{
retdouble[k]=(double *)malloc(X_dim*(sizeof(double)));

retdouble[k] = malloc(X_dim * sizeof *retdouble[k]);
// Could been *(retdouble+k) ...
if(retdouble[k]==NULL)
{
printf("Error Allocating memory ... \n");
exit(1);
}
}
...............


Now once the matrix is created some operations have to be perfomred
and then i am supposed to erase the memory the matrix is holding ...

When typing "free(retdouble)" is all of the memory (
(X_dim)*(Y_dim)*(double) ) released ????

No. You need a free() call for each malloc() call.

-Kevin

Thank you very much ....
 
L

Lorenzo Villari

Consider using the c.l.c-approved malloc idiom:
Hey "c.l.c-approved" sounds good... it could become a trademark ^^
 
A

Al Bowers

Andrew said:
Hi all, with the following code snippet I am creating a 2D matrix :

......
double **retdouble=(double **)malloc(Y_dim*(sizeof(double *)));
if(retdouble == NULL)
{
printf("Error Allocating memory ..... \n");
exit(1);
}
for (int k=0; k<Y_dim; k++)
{
retdouble[k]=(double *)malloc(X_dim*(sizeof(double)));
// Could been *(retdouble+k) ...
if(retdouble[k]==NULL)
{
printf("Error Allocating memory ... \n");
exit(1);
}
}
...............


Now once the matrix is created some operations have to be perfomred
and then i am supposed to erase the memory the matrix is holding ...

When typing "free(retdouble)" is all of the memory (
(X_dim)*(Y_dim)*(double) ) released ????
No, You are only deallocating memory that the value of retdouble
reprsents.

Review the faq queston 7.23 at
http://www.eskimo.com/~scs/C-faq/q7.23.html
Any help appreciated

To free all of the memory you will need to do:

for (int k=0; k<Y_dim; k++) free(retdouble[k]);
free(retdouble);
 
J

Jack Klein

Error output should go to stderr:

fprintf(stderr, "Error Allocating memory ... \n");

Not in all cases. In fact this knee jerk reaction is often done
incorrectly even by those who know better.

A program that essentially runs non-interactively, for example most C
compilers from a command line, has no ordinary standard output to be
separated from diagnostic messages. Sending such output to stderr
only makes capturing to a file more complex on some platforms.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
R

Richard Bos

Jack Klein said:
Not in all cases. In fact this knee jerk reaction is often done
incorrectly even by those who know better.

A program that essentially runs non-interactively, for example most C
compilers from a command line, has no ordinary standard output to be
separated from diagnostic messages. Sending such output to stderr
only makes capturing to a file more complex on some platforms.

Even so, in such cases I would prefer human error, i.e. mostly input
and/or output, failure messages to go to stdout - that is, "That input
file does not exist" should go to the user - but system error messages
like the above to stderr - the sysadmin should have a chance to see that
someone has run out of memory, because it may be his problem.

Richard
 
D

Dan Pop

In said:
Not in all cases. In fact this knee jerk reaction is often done
incorrectly even by those who know better.

The exceptions are few and far between: non-fatal errors that allow the
program execution to continue. Such errors are, indeed, better reported
together with the program's output, so that one can tell which parts of
the program's output are or may be affected by them.

Fatal errors do belong to stderr. Non-intuitively, perhaps, user prompts
also belong there, because they are not part of the program's normal
output. If you redirect stdout, you still want the user to see them.
A program that essentially runs non-interactively, for example most C
compilers from a command line, has no ordinary standard output to be
separated from diagnostic messages. Sending such output to stderr
only makes capturing to a file more complex on some platforms.

Let's see how some C compilers work:

fangorn:~/tmp 19> gcc test.c > /dev/null
test.c: In function `main':
test.c:1: parse error before `}'
fangorn:~/tmp 20> icc test.c > /dev/null
test.c
test.c(1): error: expected a ";"
int main(void) {return -1 }
^

compilation aborted for test.c (code 2)
fangorn:~/tmp 21> pgcc test.c > /dev/null
PGC-S-0036-Syntax error: Recovery attempted by inserting ';' before '}' (test.c: 1)
PGC/x86 Linux/x86 3.3-2: compilation completed with severe errors

Looks like all the C compilers I have access to on this box send their
errors to where they belong: stderr.

Dan
 
S

stelios xanthakis

Kevin Goodsell said:
Consider using the c.l.c-approved malloc idiom:

p = malloc(n * sizeof *p);

It's shorter and safer. Casting malloc's return is usually not
recommended. Search this group for more discussion of this topic.

I prefer

p = malloc (n * sizeof * p);

That confuses people.
Even better:

p = malloc (sizeof * p * n);

This is art.
 
K

Keith Thompson

message


I prefer

p = malloc (n * sizeof * p);

That confuses people.
Even better:

p = malloc (sizeof * p * n);

This is art.

If your goal is to confuse people, try:

p = malloc(sizeof * p*n);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,303
Messages
2,571,557
Members
48,359
Latest member
Raqib_121635
Top