A
ajay
Why would a new of object be created without assigning it to any of variable?
new A;
???
tx
new A;
???
tx
ajay said:Why would a new of object be created without assigning it to any of variable?
new A;
???
tx
For side effects.
It is, but this could very well not be:It's a memory leak in C++.
Siemel said:It's a memory leak in C++.
Buster said:Siemel Naran wrote:
Not necessarily.
It is, but this could very well not be:
new A(b);
Siemel said:If you overload global operator new, etc?
* "Corno said:It is, but this could very well not be:
new A(b);
Corno said:It is, but this could very well not be:
new A(b);
ajay said:Why would a new of object be created without assigning it to any of variable?
new A;
If you overload global operator new, etc?
ajay said:Why would a new of object be created without assigning it to any of variable?
new A;
???
tx
Or if its constructor registers it with some other object which takesBuster said:Or if an exception will be thrown during construction.
Richard said:Or if its constructor registers it with some other object which takes
responsibility for deleting it.
There is nothing in the latter expression that makes it less likely
to be a memory leak.
Not that a new-expression by itself is _necessarily_ a leak.
But you have to use extremely contorted mechanisms to avoid a leak, and
in that case it's much easier to use e.g. 'std::auto_ptr<A>( new A );',
or, if class A does not depend on dynamic allocation, simply 'A();'.
* Leor Zolman said:I've been trying to think of a way that any /statement/ of the form:
new A(anything-or-nothing);
(that would include /both/ Siemel and Corno's examples) would not be a leak
(assuming it doesn't fail or somehow get optimized away), and haven't been
able to.
Note that in these cases, the expression stands alone as an
expression statement; there's no additional chicanery.
And note also that
this does not contradict what you said above; I'm just contrasting your
comments with the language of the OP and those early responses in order to
try to plug a possible point of confusion.
Even if class A has no base classes or non-static data, aren't objects
obliged to have size >= 1? So no matter what you do, isn't at least that
one byte going to hanging over you up to program termination?
I've been trying to think of a way that any /statement/ of the form:
new A(anything-or-nothing);
would not be a leak
Well the simplest one has already been mentioned by others: if A::A() throws.
But that wasn't what I meant by contorted.
Here's a contorted scheme:
Here class Cleanup could conceivably be one of those dreaded "Manager"
singleton classes or some such, where objects install themselves --
which I think is very bad design, but it happens.
What I meant by extremely contorted mechanism was chicanery like the
above.
Heh. I'm often wrong. I like to boast that I'm wrong in less than 50% of
cases, but.
If there is no cleanup mechanism, yes, in the sense that one address from
the address space continues to be reserved. Not necessarily in the sense
that that address could have been used for a non-zero size object. But I
think that amounts to the same thing.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.