c++ - What happens to a dynamic array (on the heap) within a struct on the stack? -
my concern whether or not array should deallocated. here example:
typedef struct { int *values; int length; } a_struct; void foo() { a_struct mystruct; mystruct.values = new int[num]; delete[] mystruct.values; // needed? return; }
my understanding mystruct (which on stack) deleted automatically upon "return" statement. delete "values" well?
it deallocate the pointer values
, not points to - after all, a_struct
know assigned pointer? maybe it's pointer stuff allocated on stack, or array shared struct
.
so, yes, need manually deallocate (although in modern c++ "smart pointers" used manage memory).
Comments
Post a Comment