Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > delete object array question

Reply
Thread Tools

delete object array question

 
 
Sowen
Guest
Posts: n/a
 
      03-15-2005
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


 
Reply With Quote
 
 
 
 
Artie Gold
Guest
Posts: n/a
 
      03-15-2005
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
 
Reply With Quote
 
 
 
 
evaned@gmail.com
Guest
Posts: n/a
 
      03-15-2005
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.)

 
Reply With Quote
 
Larry Brasfield
Guest
Posts: n/a
 
      03-15-2005
"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.


 
Reply With Quote
 
Jeff Schwab
Guest
Posts: n/a
 
      03-15-2005
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.
 
Reply With Quote
 
codigo
Guest
Posts: n/a
 
      03-15-2005

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



 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      03-15-2005

"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


 
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
Issues using array.delete within a loop of the same array james.d.masters@gmail.com Ruby 12 03-16-2007 11:10 PM
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
Question on delete [] vs just plain delete DamonChong C++ 11 01-31-2005 01:27 PM
Passing derived class object array in place of base class object array justanotherguy63@yahoo.com C++ 9 12-03-2004 10:57 PM
Problem assigning an Array object to an Array-subclass object Richard Lionheart Ruby 27 05-04-2004 06:42 AM



Advertisments