Skip to main content
gamedev.net
🔒 Locked

delete void pointer

Started by Abominus 4 replies 1.9k views
Original Post
Abominus
Abominus
Does Standard C++ define the behavior of deleting a void pointer? I was porting some VC++ code to g++ and I got a warning message. Is it safe to ignore? My void pointer is only used to point to simple data types and not objects.
Null and Void
Null and Void
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.

SabreMan
SabreMan
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! ]
Vasialek
Vasialek
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
jeeky
jeeky
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.
Fruny
Fruny
He''s talking about void* not NULL

Documents [ GDNet | MSDN | STL | OpenGL | Formats | RTFM | Asking Smart Questions ]
C++ Stuff [ MinGW | Loki | SDL | Boost. | STLport | FLTK | ACCU Recommended Books ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.