pointer question
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]
It''s wrong, and will likely cause problems. new => delete, new[] => delete[]. That''s all there is to it.
Undefined behavior meaning anything could happen. It is bad form: this should be reason enough not to do it.
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).
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).
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''
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...
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...
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!
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!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement