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

Andrada Georgescu andradaq at gmail.com
Sat Mar 6 18:06:35 EET 2010


On Sat, Mar 6, 2010 at 5:52 PM, Dan-George Filimon <
dangeorge.filimon at gmail.com> wrote:

> const mine& mine::operator+(const mine &x) {
>        mine *helper = new mine(max(x.n, this->n));
>        helper->n = max(x.n, this->n);
>        int i;
>        for (i = 0; i < min(x.n, this->n); ++ i)
>                helper->A[i] = this->A[i] + x.A[i];
>        for (; i < helper->n; ++ i)
>                if (i >= x.n)
>                        helper->A[i] = this->A[i];
>                else
>                        helper->A[i] = x.A[i];
>        //helper->print();
>        return *helper;
> }
>
>
Nu ar trebui să aloci dinamic acolo un nou obiect, îți produce leak-uri (tu
nu vei știi niciodată să eliberezi zona alocată
pentru obiect în sine). Trebuie, după cum spunea și Andrei să îțî definești
și un copy-constructor, pe lângă assignment.
De exemplu:

mine(const mine &x) {
    this->A = new int[x.n];
    this->n = x.n;
    memcpy(this->A, x.A, x.n * sizeof(int));
}



-- 
Andrada Georgescu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rosedu.org/pipermail/cdl-studenti/attachments/20100306/0070dc69/attachment.htm>


More information about the cdl-studenti mailing list