Deleting Pointers, I'm Confused.

Started by
7 comments, last by Estauns 20 years, 1 month ago
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?
"Where genius ends, madness begins."Estauns
Advertisement
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
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?
i is 1337
Here's a sample program:
#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 =(
"Where genius ends, madness begins."Estauns
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]

This topic is closed to new replies.

Advertisement