delete object array question

Discussion in 'C++' started by Sowen, Mar 15, 2005.

  1. Sowen

    Sowen Guest

    hi,


    I have the following code

    object obj_1;
    object obj_2;
    object obj_3;

    object *objs[] = { &obj_1, &obj_2, &obj_3 };

    do I need to delete the "objs"? I tried different way, they were all wrong.
    if I don't need to delete it, why?

    thanks


    --
    Best Regards!
    Sowen Cheung
    http://com.angGoGo.com
    http://www.angGoGo.com
    http://biz.angGoGo.com
     
    Sowen, Mar 15, 2005
    #1
    1. Advertising

  2. Sowen

    Artie Gold Guest

    Sowen wrote:
    > hi,
    >
    >
    > I have the following code
    >
    > object obj_1;
    > object obj_2;
    > object obj_3;
    >
    > object *objs[] = { &obj_1, &obj_2, &obj_3 };
    >
    > do I need to delete the "objs"? I tried different way, they were all wrong.
    > if I don't need to delete it, why?
    >
    > thanks
    >
    >

    If you haven't `new'-ed it you don't `delete' it.

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
    http://it-matters.blogspot.com (new post 12/5)
    http://www.cafepress.com/goldsays
     
    Artie Gold, Mar 15, 2005
    #2
    1. Advertising

  3. Sowen

    Guest

    You can't delete it since, as Artie Gold said, you didn't create it
    with new.

    The objects will be destroyed automatically when obj_1 (and _2 and _3)
    go out of scope.

    BTW, this means that objs will be useless then, so you can't return it
    from a function. That is, the following is "illegal":

    object** objs() // because i don't know if object*[] is legal
    syntax, or, if not, what is
    {
    object obj_1;
    object obj_2;
    object obj_3;

    object *objs[] = { &obj_1, &obj_2, &obj_3 };
    return objs;
    }


    The root problem is that obj_1, obj_2, and obj_3 are created on the
    stack automatically, while new and delete operate on the heap. If you
    try to delete stuff from the stack, bad things happen. (General
    Protection Faults/Segmentation Faults, data corruption, who knows.
    Depends on your compiler and platform probably.)
     
    , Mar 15, 2005
    #3
  4. "Artie Gold" <> wrote in
    message news:...
    > Sowen wrote:
    >> hi,

    Hi.

    >> I have the following code
    >>
    >> object obj_1;
    >> object obj_2;
    >> object obj_3;
    >>
    >> object *objs[] = { &obj_1, &obj_2, &obj_3 };
    >>
    >> do I need to delete the "objs"? I tried different way, they were all wrong.
    >> if I don't need to delete it, why?

    ....
    > If you haven't `new'-ed it you don't `delete' it.


    Mr. Gold's rule is worth remembering (and observing),
    but here is the why: The objects named obj_? are
    constructed automatically, either as the block in which
    they are defined is entered, or when the program begins
    if there is no such block. The compiler arranges for
    their destructors to be run automatically also, either
    when the block is exited or the program exits. So
    you do not need to worry about it.

    Another point, (expanding on that rule): The memory
    occupied by those objects is allocated by the compiler
    (or the linker or OS, if you crave pedanticism) and is
    also deallocated by the same actor. The only time
    the programmer is responsible for deallocation is
    when the allocation has not been done automatically.
    In short, every execution of a delete should map to
    a preceding execution of a new, (at least until you
    get into exotic C++ techniques involving placement
    new, which you can put off learning for a good while.)

    --
    --Larry Brasfield
    email:
    Above views may belong only to me.
     
    Larry Brasfield, Mar 15, 2005
    #4
  5. Sowen

    Jeff Schwab Guest

    Sowen wrote:
    > hi,
    >
    >
    > I have the following code
    >
    > object obj_1;
    > object obj_2;
    > object obj_3;
    >
    > object *objs[] = { &obj_1, &obj_2, &obj_3 };
    >
    > do I need to delete the "objs"? I tried different way, they were all wrong.
    > if I don't need to delete it, why?


    They are part of some larger section of memory, which will be reclaimed
    as one big unit. The larger section of memory may be a stack frame
    (e.g. for automatic (function-local) variables), or an area allocated
    dynamically to store an instance of a class having obj_{1,2,3} as
    members, or a static block of memory that survives until the entire
    process dies.
     
    Jeff Schwab, Mar 15, 2005
    #5
  6. Sowen

    codigo Guest

    "Sowen" <> wrote in message
    news:d15jmk$ekh$...
    > hi,
    >
    >
    > I have the following code
    >
    > object obj_1;
    > object obj_2;
    > object obj_3;
    >
    > object *objs[] = { &obj_1, &obj_2, &obj_3 };
    >
    > do I need to delete the "objs"? I tried different way, they were all

    wrong.
    > if I don't need to delete it, why?


    The only occasions you need to delete an object is when you've told the
    compiler that it must relinquish the responsability of both allocation and
    deallocation. Thats exactly what "new" does.

    class Object
    {
    public:
    Object()
    {
    cout << "Object ctor invoked\n";
    }
    ~Object()
    {
    cout << "Object d~tor invoked\n";
    }
    };


    int main()
    {
    Object *p_object; // not an Object, yet

    { // anon scope

    p_object = new Object();
    Object object_temp;

    } // end of anon scope

    delete p_object;

    return 0;
    }

    Place a breakpoint at the anonymous scope's closing brace and observe
    object_temp's d~tor invocation (end of anon scope). Then keep stepping and
    observe *p_object's destruction in your output window.

    object_temp's lifetime is dependent on the anonymous scope while the Object
    at pointer p_object is the programmer's responsability.

    >
    > thanks
    >
    >
    > --
    > Best Regards!
    > Sowen Cheung
    > http://com.angGoGo.com
    > http://www.angGoGo.com
    > http://biz.angGoGo.com
    >
    >
     
    codigo, Mar 15, 2005
    #6
  7. Sowen

    Mike Wahler Guest

    "Sowen" <> wrote in message
    news:d15jmk$ekh$...
    > hi,
    >
    >
    > I have the following code
    >
    > object obj_1;
    > object obj_2;
    > object obj_3;
    >
    > object *objs[] = { &obj_1, &obj_2, &obj_3 };
    >
    > do I need to delete the "objs"?


    No.

    > I tried different way, they were all wrong.
    > if I don't need to delete it, why?


    Because you didn't allocate it with operator 'new'.

    -Mike
     
    Mike Wahler, Mar 15, 2005
    #7
    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. Replies:
    9
    Views:
    585
    justanotherguy
    Dec 3, 2004
  2. DamonChong
    Replies:
    11
    Views:
    586
    msalters
    Jan 31, 2005
  3. Replies:
    2
    Views:
    586
    Mark P
    May 9, 2005
  4. Richard Lionheart
    Replies:
    27
    Views:
    437
    Jean-Hugues ROBERT
    May 4, 2004
  5. Replies:
    12
    Views:
    215
    Robert Dober
    Mar 16, 2007
Loading...

Share This Page