Memory Leak when using memcpy in c++ -
here pseudo code:
class product{ //code }; class productdiscount:public product{ //code }; class cart{ product ** products; int noelem; };
and had put kind of products inside cart using cart += &product1;
(where product1 can of type product or productdiscount)
i did overloading += operator , here code:
void operator+=(produs * prod){ produs ** copie = new produs*[this->n]; (int = 0; < n; i++) memcpy(&copie[i], &produse[i], 4); this->n += 1; this->produse = new produs*[this->n]; (int = 0; < this->n - 1; i++) memcpy(&produse[i], &copie[i], 4); memcpy(&produse[n - 1], &prod, sizeof(copie[0])); } void operator+=(produsdiscount * prod){ produs ** copie = new produs*[this->n]; (int = 0; < n; i++) memcpy(&copie[i], &produse[i], 4); this->n += 1; this->produse = new produs*[this->n]; (int = 0; < this->n - 1; i++) memcpy(&produse[i], &copie[i], 4); memcpy(&produse[n - 1], &prod, sizeof(copie[0])); }
now, want know if there way of doing this. want know if i'm generating memory leak when use memcpy , if so, how avoid it.
Comments
Post a Comment