Re: Question about destructors
"Gus Gassmann" wrote in message
news:ba1d69e7-bd02-4c9a-9ab0-1f674fb3bc56@do4g2000vbb.googlegroups.com...
>
>I have been working with C++ for about four years now, mostly self-
>taught, and I get along reasonably well. However, last week I detected
>a memory leak in some code, and trying to locate it I realized that I
>do not know enough about destructors. I have created a number of use
>cases. How can I make sure in each case that the memory gets destroyed
>properly?
Of course this depens on the conext. But in general, use containers from the
standard library, or make your own containers to avoid the use of "naked"
new and delete.
>1. int *x[10];
std::vector<std::vector<int> > x;
>2. int *y = new int[10];
std::vector<int> y(10);
etc.
>3. Object *obj = new Object();
>
>4. Object **obj = new Object[n];
> for (int i=0; i<n; i++) obj[i] = new Object();
>
>5. Object *obj = new Object();
> obj->intArray = new int[10];
>
>6. Object **obj = new Object[n];
> for (int i=0; i<n; i++) {
> obj[i] = new Object();
> obj[i]->intArray = new int[10];
> }
>
>In advance, thanks very much.
>
|