Deleting Pointers, I'm Confused.
I''m checking out some tutorials and they do something like this:
char *pString = new char[500];
Or something like that, but when they goto delete their pointers, its done like this:
delete pString;
I thought you were supposed to use [] when you were deleting arrays?
quote:Original post by Estauns
I thought you were supposed to use [] when you were deleting arrays?
Yes you are. Just goes to show... never believe anything you read on the Internet.
You are. I wouldn''t trust those tutorials if I were you. Believe it or not, quite a few clueless newbie programmers take it upon themselves to write tutorials, long before they have the expertise to do so.
"Sneftel is correct, if rather vulgar." --Flarelocke
"Sneftel is correct, if rather vulgar." --Flarelocke
quote:Original post by Estauns
I''m checking out some tutorials and they do something like this:
char *pString = new char[500];
Or something like that, but when they goto delete their pointers, its done like this:
delete pString;
I thought you were supposed to use [] when you were deleting arrays?
Using [] in the delete is correct. If you use [] when you create the object you should use [] when you delete it.
=======
Andrew
how bad would it be if a random person *cough* were to delete
mem without the [] if it was allocated with new [SIZE]?
does it cause memory leaks or is it just the proper way of doing it?
mem without the [] if it was allocated with new [SIZE]?
does it cause memory leaks or is it just the proper way of doing it?
Here's a sample program:
According to this, you should pair new[] with delete[].
Using VC6
[edited by - nonpop on March 22, 2004 3:33:12 PM]
#include <iostream>using namespace std;class c_test{public: c_test () { cout << "test::test()\n"; } ~c_test () { cout << "test::~test ()\n"; }};int main (){ c_test* t = 0; t = new c_test[5]; delete[] t; // OK t = new c_test[5]; delete t; // Crashes return (0);}
According to this, you should pair new[] with delete[].
Using VC6
[edited by - nonpop on March 22, 2004 3:33:12 PM]
quote:Original post by SoDdit
how bad would it be if a random person *cough* were to delete
mem without the [] if it was allocated with new [SIZE]?
does it cause memory leaks or is it just the proper way of doing it?
It varies by implementation of new/delete being used. Some are just thin wrappers around malloc/free, and wouldn''t mind at all. Others can leak memory or lead to heap corruption. (Since this is a common error, implementations tend towards graceful handling, but don''t count on it.)
"Sneftel is correct, if rather vulgar." --Flarelocke
Oh okay, thanks. Thats what I thought.
These are from the Terrain tutorials at AndyPike.com, I thought it was a pretty reliable site =(
These are from the Terrain tutorials at AndyPike.com, I thought it was a pretty reliable site =(
Andy Pike''s stuff is good. Just don''t expect all the code to be perfect. Even so-called standard texts often have hundreds of errata in them. It''s good that you look out for these errors though.
[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
[ MSVC Fixes | STL Docs | SDL | Game AI | Sockets | C++ Faq Lite | Boost
Asking Questions | Organising code files | My stuff | Tiny XML | STLPort]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement