What's the difference between these two allocations?
CPolygon trgl;
CPolygon * ppoly2 = &trgl;
and
CPolygon * ppoly2 = new CPolygon();
What's the difference between these two allocations?
CPolygon trgl;
CPolygon * ppoly2 = &trgl;
and
CPolygon * ppoly2 = new CPolygon();
new
creates a dynamic object on the heap, while the first syntax create the object either in the global memory section, or on the stack.
As a consequence, if the object is allocated on the stack, it will be automatically deleted if the object goes out of scope. If the object is global, it will stay for the liftime of the process.
Object allocated with new
would have to be deleted with delete
.