delete object array question

A

Artie Gold

Sowen said:
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
 
E

evaned

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

Larry Brasfield

Artie Gold said:
Sowen said:
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.)
 
J

Jeff Schwab

Sowen said:
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.
 
C

codigo

Sowen said:
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.
 
M

Mike Wahler

Sowen said:
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
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top