Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > when is object destroyed?

Reply
Thread Tools

when is object destroyed?

 
 
Charles Herman
Guest
Posts: n/a
 
      11-06-2003
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

 
Reply With Quote
 
 
 
 
keanu
Guest
Posts: n/a
 
      11-06-2003
Charles Herman wrote in <3faabb6f_2@127.0.0.1>:

> 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 }
 
Reply With Quote
 
 
 
 
lilburne
Guest
Posts: n/a
 
      11-06-2003
Charles Herman wrote:

> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
Error:Object reference not set to an instance of an object. Suresh Kojhani ASP .Net 1 07-29-2004 12:10 PM
Error !Object reference not set to an instance of an object. !!! Help Parthiv Joshi ASP .Net 2 07-02-2004 10:28 AM
Object reference not set to an instance of an object. yysiow ASP .Net 1 07-12-2003 03:30 PM
Object reference not set to an instance of an object. Chris Fink ASP .Net 2 07-03-2003 06:48 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57