Advantages of automatic storage vs. heap storage

Using operator new (or malloc() in C) to create objects on the heap is expensive in terms of performance (allocating heap memory usually involves a long negotiation with the OS), maintenance (dynamic allocation may fail; extra code to handle such exception is required) and safety (object may be deleted more than once or not deleted at all) as demonstrated in the following example:

 void f() {string *p = new string(2000);  //initial size of 2000 chars						//...meddle with pif (condition) {	cout<<

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: