C++: Deleting the the fields of a struct that was declared on the stack -


if declare struct such one:

struct mystruct {     int* nums; };  mystruct ms; ms.nums = new int[4]; 

do need call delete ms.nums; before exiting program or member variable nums automatically deallocated because mystruct instance ms wasn't declared on heap?

yes, have delete that.. struct's default destructor won't that. delete pointer variable holds address of object , object left alone forever.

better deletion inside struct's destructor

struct mystruct {   int* nums;    public :      ~mystruct()       {         if(nums != null)             delete nums;       } }; 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -