By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,294 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

Do I really release memory ??

P: n/a
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 )

Nov 13 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Andrew wrote:
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");
exit(1);
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Nov 13 '05 #2

P: n/a
On Tue, 02 Dec 2003 07:41:49 GMT, Kevin Goodsell
<us*********************@neverbox.com> wrote:
Andrew wrote:
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");
exit(1);


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 ....
Nov 13 '05 #3

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

Nov 13 '05 #4

P: n/a


Andrew wrote:
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);
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 13 '05 #5

P: n/a
On Tue, 02 Dec 2003 07:41:49 GMT, Kevin Goodsell
<us*********************@neverbox.com> wrote in comp.lang.c:

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
Nov 13 '05 #6

P: n/a
Jack Klein <ja*******@spamcop.net> wrote:
On Tue, 02 Dec 2003 07:41:49 GMT, Kevin Goodsell
<us*********************@neverbox.com> wrote in comp.lang.c:
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.


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
Nov 13 '05 #7

P: n/a
In <gt********************************@4ax.com> Jack Klein <ja*******@spamcop.net> writes:
On Tue, 02 Dec 2003 07:41:49 GMT, Kevin Goodsell
<us*********************@neverbox.com> wrote in comp.lang.c:
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.


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #8

P: n/a
Kevin Goodsell <us*********************@neverbox.com> wrote in message news:<1n*******************@newsread2.news.pas.ear thlink.net>...
Andrew wrote:
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.


I prefer

p = malloc (n * sizeof * p);

That confuses people.
Even better:

p = malloc (sizeof * p * n);

This is art.
Nov 13 '05 #9

P: n/a
ma****@freemail.gr (stelios xanthakis) writes:
Kevin Goodsell <us*********************@neverbox.com> wrote in
message
news:<1n*******************@newsread2.news.pas.ear thlink.net>...
Andrew wrote:
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.


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

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
(Note new e-mail address)
Nov 13 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.