|
From: cplusplusquestion on 7 Jan 2008 07:33 struct B{ int e[5]; B(){e[0]=0; e[1]=1; e[2]=2; e[3]=3; e[4]=4;} int& operator[](size_t i){return e[i];} int operator[](size_t i) const {return e[i];} }; int main(){ B* a[2]; for(int i=0; i<2; i++){ B* c = new B; a[i] = c; } for(int i=0; i<2; i++) delete a[i]; cout << (*a[0])[1] << endl; return 0; } output: 1 Why pointer still there?
From: Richard Heathfield on 7 Jan 2008 07:55 cplusplusquestion(a)gmail.com said: > for(int i=0; i<2; i++) > delete a[i]; > > cout << (*a[0])[1] << endl; > > return 0; > } > > output: > 1 > > Why pointer still there? Buy cinema ticket. Go to allocated seat. Watch film. Drop sweet wrapper on seat. At end of film, leave theatre. Sneak past attendant and re-enter theatre, even though your ticket is no longer valid. Go to same seat. Why sweet wrapper still there? -- Richard Heathfield <http://www.cpax.org.uk> Email: -http://www. +rjh@ Google users: <http://www.cpax.org.uk/prg/writings/googly.php> "Usenet is a strange place" - dmr 29 July 1999
From: cplusplusquestion on 7 Jan 2008 08:07 On Jan 7, 10:55 pm, Richard Heathfield <r...(a)see.sig.invalid> wrote: > cplusplusquest...(a)gmail.com said: > > > for(int i=0; i<2; i++) > > delete a[i]; > > > cout << (*a[0])[1] << endl; > > > return 0; > > } > > > output: > > 1 > > > Why pointer still there? > > Buy cinema ticket. Go to allocated seat. Watch film. Drop sweet wrapper on > seat. At end of film, leave theatre. > > Sneak past attendant and re-enter theatre, even though your ticket is no > longer valid. Go to same seat. Why sweet wrapper still there? > > -- > Richard Heathfield <http://www.cpax.org.uk> > Email: -http://www. +rjh@ > Google users: <http://www.cpax.org.uk/prg/writings/googly.php> > "Usenet is a strange place" - dmr 29 July 1999 Does that mean the system actually released heap space?
From: Salt_Peter on 7 Jan 2008 08:30 On Jan 7, 7:33 am, cplusplusquest...(a)gmail.com wrote: > struct B{ > int e[5]; > B(){e[0]=0; e[1]=1; e[2]=2; e[3]=3; e[4]=4;} > int& operator[](size_t i){return e[i];} > int operator[](size_t i) const {return e[i];} > > }; > > int main(){ > B* a[2]; > > for(int i=0; i<2; i++){ > B* c = new B; > a[i] = c; > } > > for(int i=0; i<2; i++) > delete a[i]; > > cout << (*a[0])[1] << endl; > > return 0; > > } > > output: > 1 > > Why pointer still there? Place your home address on an envelope, mail the envelope, knock down your house and sell property, why does the envelope still have the original address on it?
From: Daniel T. on 7 Jan 2008 08:56 cplusplusquestion(a)gmail.com wrote: > Richard Heathfield <r...(a)see.sig.invalid> wrote: > > cplusplusquest...(a)gmail.com said: > > > > > for(int i=0; i<2; i++) > > > delete a[i]; > > > > > cout << (*a[0])[1] << endl; > > > > > return 0; > > > } > > > > > output: > > > 1 > > > > > Why pointer still there? > > > > Buy cinema ticket. Go to allocated seat. Watch film. Drop sweet wrapper on > > seat. At end of film, leave theatre. > > > > Sneak past attendant and re-enter theatre, even though your ticket is no > > longer valid. Go to same seat. Why sweet wrapper still there? > > Does that mean the system actually released heap space? Chances are the heap space is still dedicated to your program so that future allocations will be faster. That's not guaranteed though.
|
Next
|
Last
Pages: 1 2 Prev: A Dumb Noob Question. error: expected `;' before 'cout'??? Next: Compiling multiple files |