“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”
The second answer is correct , but in 3rd answer, i think its otherway around, memory management in stack is automatic but not in heap, so after the program execution it will be deleted and memory will be released
1. ptr = new Object;
ptr is created on the stack
Object is dynamically created on the heap
This method is suitable for large data structure or unknown size Object. It can be accessed anytime.
delete ptr is needed to prevent memory leak
2. Object o; ptr = &o;
ptr and Object are both created on the stack.
both will be cleaned up automatically.
While Object is NOT created dynamically, no more memory can be allocated for Object. Stack object is suitable for small or primitive type object.
Please point to me if there is anything incorrect.
In first stmt the object is created dynamically from heap area and the address assigned to ptr.
Second case an object is created in the stack at compile time and then the address of this existing object assigned to the ptr.
ptr is an object pointer so it can be initialized with any addres of object/dynamic.
But the problem here the dynamically allocated memory wasted upto the program termination.
new Object does not necessary creates object in the heap, operator new can be overloaded.
The main difference is that in first case object will be deleted when it will out the scope, and ptr will point to released memory (or to some trash).
The second way requires to delete object manually by it’s address, stored in ptr or it will be automatically released after program unload (not in all operating systems)
Case 1:
Object *ptr;
ptr is created on stack. 4 bytes of memory on stack is allocated for storing this ptr. But the value that this ptr holds is null (empty)
ptr = new Object;
A new object of Object class is created on the heap. The ptr now contains the value which is the address of the new object created on the heap.
You will have to do an explicit delete p to release the memory from the heap. Else this results in a memory leak.
Case 2
Object *ptr;
Same thing as above.
Object O;
A new object is created in the memory on stack. The runtime system will take care of freeing the memory of objects that are created on the stack.
Now
ptr = &O;
Now the ptr (its value) will hold the address of the object O, but this address is a stack address.
So when you get out of scope please donot do a delete. If you do a delete on ptr, the runtime system will try to deallocate / free the memory location on the stack and this will throw an exception.
The reason why this is not allowed is probably because, stack memory is any way deallocated when an object goes out of scope, so an explicit delete on stack should not be allowed.
1)ptr=new object
whenever a new object is created the ptr points to that object
2)object o;ptr=&o;
In this the pointer ptr points to the address of the object o and not the object itself
The first answer is wrong. The correct answer is:
In case 1, the object is on the heap.
In case 2, the object is on the stack.
It would create a memory leak, becase after assignment on 2 wouldn’t be possible to delete object created on heap.
The second answer is correct , but in 3rd answer, i think its otherway around, memory management in stack is automatic but not in heap, so after the program execution it will be deleted and memory will be released
1. ptr = new Object;
ptr is created on the stack
Object is dynamically created on the heap
This method is suitable for large data structure or unknown size Object. It can be accessed anytime.
delete ptr is needed to prevent memory leak
2. Object o; ptr = &o;
ptr and Object are both created on the stack.
both will be cleaned up automatically.
While Object is NOT created dynamically, no more memory can be allocated for Object. Stack object is suitable for small or primitive type object.
Please point to me if there is anything incorrect.
In first stmt the object is created dynamically from heap area and the address assigned to ptr.
Second case an object is created in the stack at compile time and then the address of this existing object assigned to the ptr.
ptr is an object pointer so it can be initialized with any addres of object/dynamic.
But the problem here the dynamically allocated memory wasted upto the program termination.
the 6rd is correct,
but in case 2, if the function return ptr, there would be some uncertain error.
new Object does not necessary creates object in the heap, operator new can be overloaded.
The main difference is that in first case object will be deleted when it will out the scope, and ptr will point to released memory (or to some trash).
The second way requires to delete object manually by it’s address, stored in ptr or it will be automatically released after program unload (not in all operating systems)
the ptr is always created on the stack.
Case 1:
Object *ptr;
ptr is created on stack. 4 bytes of memory on stack is allocated for storing this ptr. But the value that this ptr holds is null (empty)
ptr = new Object;
A new object of Object class is created on the heap. The ptr now contains the value which is the address of the new object created on the heap.
You will have to do an explicit delete p to release the memory from the heap. Else this results in a memory leak.
Case 2
Object *ptr;
Same thing as above.
Object O;
A new object is created in the memory on stack. The runtime system will take care of freeing the memory of objects that are created on the stack.
Now
ptr = &O;
Now the ptr (its value) will hold the address of the object O, but this address is a stack address.
So when you get out of scope please donot do a delete. If you do a delete on ptr, the runtime system will try to deallocate / free the memory location on the stack and this will throw an exception.
The reason why this is not allowed is probably because, stack memory is any way deallocated when an object goes out of scope, so an explicit delete on stack should not be allowed.
Leave an Answer/Comment