Original Post
🔒 Locked
delete void pointer
Advertisement
It will free the memory, yes. However, in the case that it does point to something with a destructor, the destructor will not be called. If you can avoid doing it, you probably should.


It doesn''t define the behaviour. IOW, it''s undefined. To extend this, calling delete on a pointer that is not of the new''d type, unless it is a base of the new''d type, results in undefined behaviour.
[ C++ FAQ Lite | ACCU | Boost | Python | Agile Manifesto! ]
[ C++ FAQ Lite | ACCU | Boost | Python | Agile Manifesto! ]
Hi, all!
I wonder what does it means "void" pointer? If u set pointer to NULL, manually it is SAFE to delete it. ( According to J. Liberty )
char *p = new char[100];
delete []p;
p = NULL;
delete p;
On Linux g++, with warning on, this goes without problems. In fact setting pointers to NULL is really good idea
This all is IMHO 
Lekha
I wonder what does it means "void" pointer? If u set pointer to NULL, manually it is SAFE to delete it. ( According to J. Liberty )
char *p = new char[100];
delete []p;
p = NULL;
delete p;
On Linux g++, with warning on, this goes without problems. In fact setting pointers to NULL is really good idea
Lekha
Just a comment on the last line of the posted code:
char *p = new char[100];
delete []p;
p = NULL;
delete p; <- This line is unnecessary, since you already deleted p AND you have nullified the address of the allocated memory.
Anytime you do this-
pFoo = NULL;
delete pFoo;
-the only thing you have accomplished is created a memory leak.
char *p = new char[100];
delete []p;
p = NULL;
delete p; <- This line is unnecessary, since you already deleted p AND you have nullified the address of the allocated memory.
Anytime you do this-
pFoo = NULL;
delete pFoo;
-the only thing you have accomplished is created a memory leak.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement