• ### Announcements

#### Archived

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

# Deleting Pointers, I'm Confused.

## Recommended Posts

Estauns    103
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?

##### Share on other sites
Guest Anonymous Poster
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.

##### Share on other sites
Sneftel    1788
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

##### Share on other sites
acraig    471
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

##### Share on other sites
SoDdit    157
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?

##### Share on other sites
nonpop    122
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]

##### Share on other sites
Sneftel    1788
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

##### Share on other sites
Estauns    103
Oh okay, thanks. Thats what I thought.

These are from the Terrain tutorials at AndyPike.com, I thought it was a pretty reliable site =(