when is object destroyed?

Discussion in 'C++' started by Charles Herman, Nov 6, 2003.

  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
    Charles Herman, Nov 6, 2003
    #1
    1. Advertising

  2. Charles Herman

    keanu Guest

    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 }
    keanu, Nov 6, 2003
    #2
    1. Advertising

  3. Charles Herman

    lilburne Guest

    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.
    lilburne, Nov 6, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. enrique
    Replies:
    8
    Views:
    547
    Kevin McMurtrie
    Apr 29, 2005
  2. Anon Email

    Is this object destroyed?

    Anon Email, Dec 7, 2003, in forum: C++
    Replies:
    2
    Views:
    315
    Anon Email
    Dec 8, 2003
  3. Ioannis Vranos
    Replies:
    9
    Views:
    314
    Ioannis Vranos
    May 16, 2005
  4. Nick Jacobson

    Enumerate object is destroyed by casting?

    Nick Jacobson, Aug 25, 2004, in forum: Python
    Replies:
    2
    Views:
    287
    Shalabh Chaturvedi
    Aug 25, 2004
  5. Raga
    Replies:
    0
    Views:
    294
Loading...

Share This Page