From: Richard Heathfield on 27 Nov 2009 05:11 In <e4c55192-3add-4776-b9f1-2c7121916daf(a)o10g2000yqa.googlegroups.com>, n00m wrote: > Because it's False: > >>>> w = 42 >>>> x = 6 >>>> y = w * x >>>> z = y - x >>>> >>>> z == w > False Right, because... (see below) >>>> > > >> ... with your class code ... > > "[]" missed there. Right. The delete operator complements the new operator (rather like the subtraction operator complements the addition operator). And the delete[] operator complements the new[] operator (rather like the division operator complements the multiplication operator). Trying to reverse a new[] with a delete is rather like trying to reverse a * with a -. > I'm still on a curve of accustoming myself to syntax etc. > Logically it's obvious that "delete" was supposed to free > *all* memory allocated for array "arr". The symmetry is more obvious if it's spelled out like this: X = new Y; delete X; X = new Y[6]; delete [] X; -- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ "Usenet is a strange place" - dmr 29 July 1999 Sig line vacant - apply within
From: n00m on 27 Nov 2009 08:59 Thanks, Richard.
From: Ulrich Eckhardt on 27 Nov 2009 12:45 n00m wrote: > I would like not to see here words like "homework", "RTFM" etc. I'll help you even with your homework, but not if you don't show an effort first. You just dumped that question with the code, which is equivalent to the amount of context people get for homework, so this looks like homework. What made you suspect it could be wrong at all? What didn't work as you expected? All these things missing are what make this look like homework. Uli
From: n00m on 27 Nov 2009 15:51 On Nov 27, 7:45 pm, Ulrich Eckhardt <dooms...(a)knuut.de> wrote: > What made you suspect it could be wrong at all? What didn't work as you > expected? Just came across a post (in other place) of an applicant to a job. He got some tests (and failed to pass them). The 1st task was: point out all "problems" in the code (see below): class Foo { public: Foo(int j) { i = new int[j]; } ~Foo() { delete i; } private: int* i; }; class Bar: Foo { public: Bar(int j) { i = new char[j]; } ~Bar() { delete i; } private: char* i; }; void main() { Foo* f = new Foo(100); Foo* b = new Bar(200); *f = *b; delete f; delete b; }
From: LR on 27 Nov 2009 16:09 n00m wrote: > P.S. > I would like not to see here words like "homework", "RTFM" etc. > If you don't know e.g. Python, even 5 books on it will not help > you in a minute or so to find/to guess the right answer for this > (no matter how smart you are (or are not)): Regarding your question about delete, http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.12 http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.13 http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.14 LR
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: Annoncing Gamdel, The Game Description Language Next: Accelerated C++ Chapter 5 Exercise 1 |