From: apsh AT ecr DOT mu DOT oz DOT au (Alistair_P SHILTON) Newsgroups: comp.os.msdos.djgpp Subject: Re: Strange behaviour of new/delete Date: 7 Jun 2000 07:27:55 GMT Organization: Computer Science, University of Melbourne Lines: 169 Message-ID: <8hkthr$kda$1@mulga.cs.mu.OZ.AU> References: <8hknqj$gki$1 AT mulga DOT cs DOT mu DOT OZ DOT AU> <393DF410 DOT 93824118 AT earthlink DOT net> NNTP-Posting-Host: gromit.ecr.mu.oz.au X-Trace: mulga.cs.mu.OZ.AU 960362875 20906 128.250.61.61 (7 Jun 2000 07:27:55 GMT) X-Complaints-To: usenet AT cs DOT mu DOT OZ DOT AU NNTP-Posting-Date: 7 Jun 2000 07:27:55 GMT X-Newsreader: TIN [version 1.2 PL2] To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com Is that a no :) ? Thanks anyway - its just that I've been trying to work this out for ~4 weeks now with no success, so I thought, just maybe... oh well, back to the drawing board. I'll try your suggestion of C++ing it up a bit more and see if it helps. Sorry for not posting the full code, but the program is quite large (~6000 over 4 files), and the new and delete statements are quite distant from one another. ps - how do you ckeck for new error? Sorry if that's a dumb question, but I'm relatively new to C++ (I usually do C). Thanks again. Martin Ambuhl (mambuhl AT earthlink DOT net) wrote: : Alistair_P SHILTON wrote the text quoted below my answer: : /* mha: code posted by apsh AT ecr DOT mu DOT oz DOT au (Alistair_P SHILTON), turned : into a real program. He is concerned with "Strange behaviour of : new/delete" and asks: : "I was wondering if anyone else has encountered trouble with : new/delete. I have a function which allocates memory with new as : follows:" : I have obviously added the 'int main() {' 'and return 0; }' program : bracketing. All references to NULL have been changed to 0, in : conformance with most C++ prescriptive stylists, including BS. : a, b, c, i, removal, and size were never declared in the original : code. I have declared them as they appear, no matter whether this is : stylistically best. A value for i (5 in this case) and for size (12) : were arbitrarily added since it needs initialization. : I have included so the printfs become legal. : After all these things, to have a compilable program, there : are no compilation or runtime errors. Since the apsh's question now : has no referrent, I would suggest that his actual program (which he : has not shown us) has some apsh-generated errors, perhaps some of : which are corrected by the above. : The tests for new failure are misguided. new does not act like : malloc(). They have been removed. : */ : #include : int main() : { : double **res = new double *[3]; : size_t i = 5, size = 12; : double *a = new double[i]; : double *b = new double[1]; : double *c = new double[size - i + 1]; : res[0] = a; : res[1] = b; : res[2] = c; : double **removal = new double *[3]; : removal[0] = a; : removal[1] = b; : removal[2] = c; : /* apsh - For simplicity, I've allocated 1 double too much to a and : c. Later on I delete as follows: (res has since become removal) : */ : delete removal[0]; : printf("0 down\n"); : delete removal[1]; : printf("1 down\n"); : delete removal[2]; : printf("2 down\n"); : printf("freed partial removal\n"); : delete removal; : printf("free mem\n"); : /* apsh: (ignore the printf's - I was debugging). The odd : behaviour is this - if I don't actually use a or c (don't put : anything in them), then it hangs at delete removal[2] (even : though a non zero amount of memory was allocated)!! I've checked : on unix, and it works fine - so is this a known bug with djgpp, : or am I missing something? */ : /* mha: you are obviously missing something. Perhaps it is legal : code. */ : return 0; : } : > : > I was wondering if anyone else has encountered trouble with new/delete. : > I have a function which allocates memory with new as follows: : > : > double **res; : > : > res = new double *[3]; : > : > if ( res == NULL ) { return NULL; } : > : > a = new double[i]; : > if ( a == NULL ) { return NULL; } : > : > b = new double[1]; : > if ( b == NULL ) { return NULL; } : > : > c = new double[size-i+1]; : > if ( c == NULL ) { return NULL; } : > : > res[0] = a; : > res[1] = b; : > res[2] = c; : > : > For simplicity, I've allocated 1 double too much to a and c. Later on I delete : > as follows: : > : > (res has since become removal) : > : > delete removal[0]; : > printf("0 down\n"); : > delete removal[1]; : > printf("1 down\n"); : > delete removal[2]; : > printf("2 down\n"); : > : > printf("freed partial removal\n"); : > delete removal; : > : > printf("free mem\n"); : > : > (ignore the printf's - I was debugging). The odd behaviour is this - if I : > don't actually use a or c (don't put anything in them), then it hangs at : > delete removal[2] (even though a non zero amount of memory was allocated)!! : > : > I've checked on unix, and it works fine - so is this a known bug with djgpp, : > or am I missing something? : > : > ---------------------------------------------------- : > : > Thanks : > : > Alistair Shilton : > : > http://www.ee.mu.oz.au/pgrad/apsh : -- : Martin Ambuhl mambuhl AT earthlink DOT net : What one knows is, in youth, of little moment; they know enough who : know how to learn. - Henry Adams : A thick skin is a gift from God. - Konrad Adenauer -- ---------------------------------------------------- Alistair Shilton http://www.ee.mu.oz.au/pgrad/apsh