Reputation: 163
I'm learning C++ and I am trying to understand when to assign a pointer to a base class to an object created using the new
keyword as opposed to creating an object normally and setting a pointer equal to the address of the object?
Effectively is there any difference in the methods implemented below.
#include<iostream>
using namespace std;
class Base
{
public:
virtual void show() { cout<<" In Base \n"; }
};
class Derived: public Base
{
public:
void show() { cout<<"In Derived \n"; }
};
int main(void)
{
Base *bp = new Derived();
bp->show();
Derived d1;
Base *bp = &d1;
bp->show();
return 0;
}
Upvotes: 2
Views: 10612
Reputation: 36597
The only difference - in your code at least - is in how lifetime is managed,
The first object is dynamically allocated (created when the programmer chooses, at run time) and will continue to exist until a corresponding delete
expression. Since there is no delete
expression, the object will never be released (although modern operating systems will typically reclaim memory as the program exits, that is not strictly required). If you were to add a statement that does delete bp
, it will have undefined behaviour, since Base
does not have a virtual destructor.
The second case is a variable of automatic storage duration, so ceases to exist when the enclosing block ends. It will cease to exist as main()
returns.
In your code as shown, there will be no visible difference in output. That does not mean the two cases work the same way.
Upvotes: 2
Reputation: 857
The difference is in how your memory is being allocated. If you use your first definition, you are using memory from the heap
, which is essentially boundless (within system constraints obviously), if you allocate the object the second way you are using the stack
which has a finite set of memory allocated to it for a given process running it. An example where this could become an issue is when you are creating a ton of objects on your stack (E.g., for a video game) and that stack becomes full, then you will get a stack over flow
error returned. This has happened to me once, and then you must leverage the power and knowledge of the heap.
Upvotes: 0
Reputation: 181280
The formula is pretty simple:
1) If you use new
, you are allocating memory (on the heap) for the newly created object. Your variable (pointer) points to that memory region. You should delete
the created object (through its pointer) at some point yourself.
Referring to your example:
Base *bp = new Derived();
bp->show();
delete bp;
bp = NULL; // this is good practice, NULLing the pointer after deleting
// the referenced memory
2) If you just have a (non pointer) local variable, then the memory is automatically allocated for it on the function's stack. Memory is automatically freed when the execution point leaves that function. If you just have a pointer to a local variable, you don't need to free (delete) the memory.
Derived d1;
Base *bp = &d1;
bp->show();
// no need to free
However, it's better to do just this:
Derived d1;
d1.show();
Simpler.
Upvotes: 2