Archived

This topic is now archived and is closed to further replies.

pointer question

This topic is 5304 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was just wondering the validity and possible pitfalls of doing the following: char * pMyPointer; pMyPointer = new char; // Do stuff... delete[] pMyPointer; Since I delete the memory with the braces, will that cause a memory fault? Just curious. == No, this is NOT the same question that is 2 or so threads down! == [edited by - zackriggle on June 2, 2003 7:10:24 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It''s wrong, and will likely cause problems. new => delete, new[] => delete[]. That''s all there is to it.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
just use:

pMyPointer = new char[1];

This does the same thing, but works properly with delete[] pMyPointer. I just wanted to point out that MSVC (and probably most implementations) work fine swaping delete[] and delete and new and new[]. I beleive most implementations simply forward new to new[1], and delete -> delete[], since it''s the same exact thing, it''d be pretty pointless to write 2 seperate implementations. It IS undefined, and should not be mixed, but I don''t think it''d cause any problems on most compilers (not an excuse to do it incorrectly though, just do it right, and then you don''t have to worry about it).

Share this post


Link to post
Share on other sites
quote:
Anonymous Poster: I don''t think it''d cause any problems on most compilers


Considering we don''t know who you are, your opinion isn''t worth anything.

''But boss, the anonymous poster said they thought that most compilers did the same as MSVC. Anyone could make the same mistake''

Share this post


Link to post
Share on other sites
Just to prolong the point...

If somebody overloaded the new and delete operators for a class, new will use the custom new operator, but delete[] will not use the custom delete operator. Who knows what that might do...

Share this post


Link to post
Share on other sites
NEVER mix new with delete[] or new[] with delete. A lot of compilers when creating an array will allocate some extra data, store how many elements there are at the start, and return a pointer a few bytes ahead of where the actual start is. Hence delete[] will end up reading data it isn''t supposed to, depending on the garbage it will probably attempt to call the destructors for a ton of objects that don''t exist, and attempt to delete a few bytes before the actual pointer you gave it.

SO DON''T EVER DO IT! EVEN IF IT APPEARS TO WORK! EVEN IF IT DOESN''T CRASH! EVEN IF IT HAS NO CONSTRUCTOR! EVEN IF AN ANONYMOUS POSTER SAYS IT IS ALRIGHT! IT IS UNDEFINED! IT IS A TIME BOMB WAITING TO HAPPEN! YOU COULD END UP KILLING US ALL! NEVER EVER DO IT!

Share this post


Link to post
Share on other sites