Archived

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

Estauns

Deleting Pointers, I'm Confused.

Recommended Posts

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 this post


Link to post
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
Oh okay, thanks. Thats what I thought.

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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites