I have a class with a method allocating some memory using new operator, can other members of class can excess that memory? How about outside the class?
In general, dynamically allocated memory will "live" as long as you don't explicitly deallocate it with delete
or delete[]
.
As far as the scope is concerned: there's no such a thing as a scope for dynamically allocated memory. We usually talk about scope for automatic memory (because automatic memory will be deallocated at the end of the scope). The rules for the scope of the pointer to the dynamically allocated memory, on the other hand, are the same as any other automatic object.
In the C++ Standard (N3797 draft) this is specified at §3.7.4/1:
Objects can be created dynamically during program execution (1.9), using new-expressions (5.3.4), and destroyed using delete-expressions (5.3.5). A C++ implementation provides access to, and management of, dynamic storage via the global allocation functions
operator new
andoperator new[]
and the global deallocation functions operatordelete
andoperator delete[]
.
Untill you destory the object exist it remains in memory, furthermore if you provide a pointer to that object (or more common use get/set functions) it can be accessed from outside the class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With