445,918 Members | 2,294 Online
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
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

 P: n/a On Tue, 02 Dec 2003 07:41:49 GMT, Kevin Goodsell 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 notrecommended. 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 thatare endorsed by the standard: 0, EXIT_SUCCESS, or EXIT_FAILURE. } for (int k=0; k

 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

 P: n/a On Tue, 02 Dec 2003 07:41:49 GMT, Kevin Goodsell 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 wrote: On Tue, 02 Dec 2003 07:41:49 GMT, Kevin Goodsell 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 Jack Klein writes: On Tue, 02 Dec 2003 07:41:49 GMT, Kevin Goodsell 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 doneincorrectly 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 Ccompilers from a command line, has no ordinary standard output to beseparated from diagnostic messages. Sending such output to stderronly 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 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 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 San Diego Supercomputer Center <*> 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.