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