-3

What's the difference between these two allocations?

CPolygon trgl;
CPolygon * ppoly2 = &trgl;

and

CPolygon * ppoly2 = new CPolygon();
user2949483
  • 103
  • 1
  • 1
  • 8

1 Answers1

2

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.

Devolus
  • 21,661
  • 13
  • 66
  • 113