new and delete operators

S

sarathy

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
 
R

Rolf Magnus

sarathy said:
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.
 
S

Scott McPhillips [MVP]

sarathy said:
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.
 
R

R Samuel Klatchko

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

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,048
Latest member
verona

Latest Threads

Top