Re: Can static memory be deleted
Zedzed wrote:
I have a very simple question: Can a static pointer be deleted?
Yes.
For
example:
class X
{
//some implementation details
};
int main(int argc, char* argv[])
{
static X* p = new X();
.
.
.
delete p;
}
I know this code is a bit convoluted and many people will have comments
about that fact that the system will clean up the static memory
The important question is - *what* exactly is static here?
The only thing that is static is the pointer and indeed, the system,
with extreme care, will ensure that the pointer is cleaned up. But the
object of type X, which was allocated dynamically, has nothing to do
with it - it will not be automagically cleaned up.
Having said that, deleting the static pointer to dynamic object is very
likely solving the wrong problem. I guess you want to do this instead:
void foo()
{
static X object;
// ...
}
Then, the object will be cleaned up at the end of the program.
Consider using some smart pointer to get the benefits of these two
approaches, if you really need to allocate the object dynamically.
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]