473,398 Members | 2,427 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,398 software developers and data experts.

new and delete operators

Hi,
Need a clarification wrt new and delete operators.
Consider the 2 code snippets.

1.

{
Object *obj = new Object();
....
obj->method1();
obj->method2();
....
delete obj;
}

2.

{
Object obj;
.....
obj.method1();
obj.method2();
}

Is there any difference in the memory allocation technique in the 2
cases. Doing "new" will automatically allocate space for the required
object and will return the pointer to the allocated space.

If that is the case for 1, then how will be memory allocated to the obj
in case 2. Anyhow memory would have to be allocated to these objects
too using some techniq. Then why no delete is used here to reclaim the
used space. If the answer is "the destructor takes care of it", then
why wont the same destructor take care of the mem release in case1. Why
an explicit delete is needed in the 1st case?

Regards,
Sarathy

Jul 30 '06 #1
3 1423
sarathy wrote:
Hi,
Need a clarification wrt new and delete operators.
Consider the 2 code snippets.

1.

{
Object *obj = new Object();
...
obj->method1();
obj->method2();
...
delete obj;
}

2.

{
Object obj;
....
obj.method1();
obj.method2();
}

Is there any difference in the memory allocation technique in the 2
cases.
Yes.
Doing "new" will automatically allocate space for the required
object and will return the pointer to the allocated space.
Yes.
If that is the case for 1, then how will be memory allocated to the obj
in case 2. Anyhow memory would have to be allocated to these objects
too using some techniq.
The C++ standard isn't really specific about this. In one case, it's
allcoated on the free storage, in the other, on the automatic storage.
Dynamic allocation usually takes a bit more time and space.
Then why no delete is used here to reclaim the used space.
Automatic objects (i.e. those defined locally in a function) are destroyed
automatically (hence the name) when the function returns or throws.
If the answer is "the destructor takes care of it", then why wont the
same destructor take care of the mem release in case1.
The destructor doesn't take care of it. The destructor will be called as
part of object destruction in either case.
Why an explicit delete is needed in the 1st case?
Well, one advantage of new/delete is that you can control the object's
lifetime. Automatic objects are always destroyed when they go out of scope
(e.g. returning from funciton), but what if you want the object to live
longer than that? Then you can allocate it dynamically, but of course, you
must now handle the destruction yourself, because the runtime system
doesn't know when you want the object to be destroyed.

Jul 30 '06 #2
sarathy wrote:
Hi,
Need a clarification wrt new and delete operators.
Consider the 2 code snippets.

1.

{
Object *obj = new Object();
...
obj->method1();
obj->method2();
...
delete obj;
}

2.

{
Object obj;
....
obj.method1();
obj.method2();
}

Is there any difference in the memory allocation technique in the 2
cases. Doing "new" will automatically allocate space for the required
object and will return the pointer to the allocated space.

If that is the case for 1, then how will be memory allocated to the obj
in case 2. Anyhow memory would have to be allocated to these objects
too using some techniq. Then why no delete is used here to reclaim the
used space. If the answer is "the destructor takes care of it", then
why wont the same destructor take care of the mem release in case1. Why
an explicit delete is needed in the 1st case?

Regards,
Sarathy
The explicit delete is "needed" in the first case so you can control
when the object is deleted. It's a feature!

In the second case, the delete is performed automatically by the "}"
when the object goes out of scope. It uses a temporary storage area
(typically the stack) that is very efficient to use within a function's
scope.

--
Scott McPhillips [VC++ MVP]

Jul 30 '06 #3
sarathy wrote:
1.

{
Object *obj = new Object();
...
obj->method1();
obj->method2();
...
delete obj;
}

2.

{
Object obj;
....
obj.method1();
obj.method2();
}

Is there any difference in the memory allocation technique in the 2
cases.
Besides the issues covered by the other replies, a subtle difference
between the two revolves around running out of memory.

For case 1, new will throw std::bad_alloc when it cannot allocate memory
to hold the object. Assuming the runtime implementation of exceptions
work when there is no memory left, you should then be able to handle
this condition in a portable way by catching the exception.

For case 2, the standard does not specify the behavior for out of
memory. Depending on the platform, I have seen stack overflow either
cause the program to crash or to corrupt memory.

samuel
Aug 1 '06 #4

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

Similar topics

2
by: Ian McBride | last post by:
(was: delete() confusion) I have a class with multiple base classes. One of these base classes (base1) has its own new/delete operators and nothing else. Another base class (base 2) has a...
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...
3
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. I am...
20
by: Ioannis Vranos | last post by:
When we use the standard placement new operator provided in <new>, and not a definition of owr own, isn't a call to placement delete enough? Consider the code: #include <new>
4
by: GianGuz | last post by:
Global new and delete operators can be overloaded to suite particulars needs. Typically they are overloaded to insert useful debugging/trace informations. What I would to discuss here concerns the...
3
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >>...
9
by: groleo | last post by:
Hi list. Simple question: is it possible to override the global new/delete operators, without using malloc/free? I mean something in the ideea of the code below, which doesnt work cause of...
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...
1
by: dilabox | last post by:
Hello, I have overloaded the global new, delete, new and delete operators inside a "static library". The library uses private memory allocation routines that must not be accessible from other...
3
by: simbasaurus | last post by:
Hello! I am working on a really big project, in which the global new and delete operators can be overloaded, by including the file "overloadedops.h". The graphical interface was written using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.