[cdl-studenti] [OOP] Eliberare automata de memorie.

marius Dragus mdragus at gmail.com
Sat Mar 6 17:47:39 EET 2010


Dap, a mers asa, merci.

2010/3/6 Andrei Soare <andrei.soare at gmail.com>

> Se elibereaza, intr-adevar, dar dupa ce se copiaza in z. Se va apela apoi
> destructorul lui y si in final si pe cel al lui z si intr-adevar o sa faca
> free de 2 ori pe aceeasi zona de memorie.
>
> Ca sa nu faca free de 2 ori pe aceeasi zona de memorie, trebuie neaparat
> implementat copy constructor si copy assignment operator sa aloce o zona
> noua de memorie pentru toate datele alocate dinamic din clasa.
>
> Mereu cand o clasa are membri alocati dinamic trebuie implementat de mana
> copy constructor si copy assignment operator.
>
> -----Original Message-----
> From: marius Dragus
>
> Eu am testat si se cam elibereaza. Se apeleaza destructorul pentru
> variabila
> locala y.
>
> 2010/3/6 Dan-George Filimon <dangeorge.filimon at gmail.com>
>
> > Nu se elibereaza y cand zici tu la pasul 2 din cate inteleg eu.
> >
> >
> > On Mar 6, 2010, at 4:41 PM, marius Dragus wrote:
> >
> > Nu e ok. Varianta asta e gresita din mai multe motive (produce double
> free,
> > copiaza memorie inexistenta).
> >
> > z= x+y;
> >
> > 0. Se apeleaza operatorul + pentru x
> > 1. Se aloca y  in operator+.
> > 2. Se apeleaza destructorul pentru y
> > 3. Se apeleaza operatorul de asignare pentru z ( dar partea dreapta a
> fost
> > eliberata deja la pasul 2 -- deci e gresit)
> > 4. Se apelte ca fiecare obiect aloca dinamic un array, iar o operatie
> >>> de genul x =a +b+c ar cauza memory leak-uri.
> >>> >
> >>> > Cum s-ar putea face asta?
> >>> >
> >>> > Mai jos este codul. Mentin un contor al
> constructorilor/destructorilor,
> >>> si se vede evident ca se obtin memory leakuri.
> >>> >
> >>> > Mersi,
> >>> > Marius
> >>> >
> >>> > /*************************CODE*********************/
> >>> >
> >>> > #include <iostream>
> >>> >
> >>> > int count = 0;
> >>> > using namespace std;
> >>> >
> >>> > struct mine
> >>> > {
> >>> >     int * u;
> >>> >     mine()
> >>> >     {
> >>> >     u = new int[10000];
> >>> >     count++;
> >>> >     }
> >>> >
> >>> >     ~mine()
> >>> >     {
> >>> >     delete[] u;
> >>> >     count--;
> >>> >     }
> >>> >
> >>> >     mine & operator=(const mine & x)
> >>> >     {
> >>> >     //cout << "assign" << endl;
> >>> >     u = x.u;
> >>> >     }
> >>> >
> >>> >
> >>> > };
> >>> >
> >>> > mine& operator+(const mine & x, const mine &y)
> >>> > {
> >>> >     mine * ret = new mine;
> >>> >     return *ret;
> >>> > }
> >>> >
> >>> > int main()
> >>> > {
> >>> >
> >>> >     mine a, b, c, d;
> >>> >     mine e;
> >>> >
> >>> >
> >>> >     for (int i = 0; i<10; i++)
> >>> >     {
> >>> >     e = a + b + c + d;
> >>> >
> >>> >     }
> >>> >
> >>> >     cout << count << "\n"; // afiseaza 35 desi ar trebui sa fie 5
> >>> >
> >>> >     return 0;
> >>> > }
> >>> >
> >>> > _______________________________________________
> >>> > cdl-studenti mailing list
> >>> > cdl-studenti at lists.rosedu.org
> >>> > http://lists.rosedu.org/listinfo/cdl-studenti
> >>>
> >>> _______________________________________________
> >>> cdl-studenti mailing list
> >>> cdl-studenti at lists.rosedu.org
> >>> http://lists.rosedu.org/listinfo/cdl-studenti
> >>>
> >>
> >> _______________________________________________
> >> cdl-studenti mailing list
> >> cdl-studenti at lists.rosedu.org
> >> http://lists.rosedu.org/listinfo/cdl-studenti
> >>
> >>
> >>
> >> _______________________________________________
> >> cdl-studenti mailing list
> >> cdl-studenti at lists.rosedu.org
> >> http://lists.rosedu.org/listinfo/cdl-studenti
> >>
> >
> > _______________________________________________
> > cdl-studenti mailing list
> > cdl-studenti at lists.rosedu.org
> > http://lists.rosedu.org/listinfo/cdl-studenti
> >
> >
> >
> > _______________________________________________
> > cdl-studenti mailing list
> > cdl-studenti at lists.rosedu.org
> > http://lists.rosedu.org/listinfo/cdl-studenti
> >
>
> _______________________________________________
> cdl-studenti mailing list
> cdl-studenti at lists.rosedu.org
> http://lists.rosedu.org/listinfo/cdl-studenti
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rosedu.org/pipermail/cdl-studenti/attachments/20100306/20c13433/attachment.htm>


More information about the cdl-studenti mailing list