when is object destroyed?

C

Charles Herman

I have the following loop:

for (int i = 0; i < n; ++i)
{
CClass newObject( i );
//
// lines of code
//
}

Since a new object is being created (is it?) for every iteration, when is
the previous object being destroyed, or is it? If not, how do I destroy
it? Do I need to, if I don't will this lead to a memory leak?

-charles
 
K

keanu

Charles Herman wrote in said:
I have the following loop:

for (int i = 0; i < n; ++i)
{
CClass newObject( i );
//
// lines of code
//
}

Since a new object is being created (is it?) for every iteration, when is
the previous object being destroyed, or is it? If not, how do I destroy
it? Do I need to, if I don't will this lead to a memory leak?

-charles

it will be destroyed at the end of the scope
at the }
 
L

lilburne

Charles said:
I have the following loop:

for (int i = 0; i < n; ++i)
{
CClass newObject( i );
//
// lines of code
//
}

Since a new object is being created (is it?) for every iteration, when is
the previous object being destroyed, or is it? If not, how do I destroy
it? Do I need to, if I don't will this lead to a memory leak?

Its created each time though the loop and each time through
too (closing brace). Can be a potential bottleneck,
particularly if it allocates any memory, but you won't know
for sure unless you profile it.
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top