pointer question

Started by
6 comments, last by zackriggle 20 years, 10 months ago
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]
Advertisement
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.
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
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).
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''
Since chars don''t have destructors, it shouldn''t be a problem.
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...
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!
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!
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.

This topic is closed to new replies.

Advertisement