473,418 Members | 2,267 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

delete object array question

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
Jul 23 '05 #1
6 2451
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
Jul 23 '05 #2
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.)

Jul 23 '05 #3
"Artie Gold" <ar*******@austin.rr.com> wrote in
message news:39*************@individual.net...
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: do***********************@hotmail.com
Above views may belong only to me.
Jul 23 '05 #4
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.
Jul 23 '05 #5

"Sowen" <so***@anggogo.com> wrote in message
news:d1**********@canopus.cc.umanitoba.ca...
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
Jul 23 '05 #6

"Sowen" <so***@anggogo.com> wrote in message
news:d1**********@canopus.cc.umanitoba.ca...
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

Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Pelle | last post by:
Hello all, I have to admit, that the idea that occurred to me recently is weird. It was somehow inspired by the huge response on the "delete operator" thread, but goes into a somewhat different...
1
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there...
11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
15
by: Roy Smith | last post by:
I understand that "delete xp" deletes a scalar object and "delete xp" deletes an array of objects, but what I don't understand is why you need to tell the compiler which you're doing. When you...
11
by: DamonChong | last post by:
Hi, I am new to c++. I recently spend an enormous among of time troubleshooting a seeminly innocuous piece of code. Although I narrow down this piece of code as the culprit but I don't...
30
by: jimjim | last post by:
Hello, This is a simple question for you all, I guess . int main(){ double *g= new double; *g = 9; delete g; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl; *g =...
12
by: ravinderthakur | last post by:
hi experts, i have few questions regarding the delete operator in c++. why does c++ have to operators for deleting memeory viz delete and delete. why cannnot delete be used insted of...
9
by: Money | last post by:
If I allocate memory like this int *ptr = new int; Can I apply delete ptr; instead of delete ptr; since I am only allocating memory for 1 integer.
29
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
29
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I remembered delete is implemented through operator overloading, but I am not quite clear. Could anyone recommend some links about how delete is implemented so that I can...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.