• Advertisement
Sign in to follow this  

Creating & deleting pointers

This topic is 2001 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Oh I've figured out why the following was evaluating to all die.

for ( vector<Animal>::iterator iter = myAnimals.begin(); iter != myAnimals.end(); /* empty */ )
{
if ( iter->Check4Death() )
{
cout << iter->GetName() << " has died!\n";
iter = myAnimals.erase( iter );
} else {
iter ++;
}
}
};

it's because the constructor call was creating pointers to the member functions and killing the code, now i've swapped it all back to being normal variables the code it working fully. Thanks for all the help guys, the answers were in there. They were just dotted around a bit.

Share this post


Link to post
Share on other sites
Advertisement
[quote name='BeerNutts' timestamp='1343176721' post='4962786']You don't need to NULL them out.[/quote]
Indeed, and to get this point across, NULLing out the pointers in the destructor after freeing the memory they pointed to is like using white-out over your text on a piece of paper just before you put it through a shredder.
If you're about to shred it, then there's no point in using white-out.

Share this post


Link to post
Share on other sites
[quote name='iMalc' timestamp='1343371761' post='4963539']
If you're about to shred it, then there's no point in using white-out.
[/quote]

I use this technique now and then. In principle, you are perfectly correct. In practice, this technique helps you find cases where you are using dangling pointers. I tend to make those mistakes myself less and less with the years (though it still happens), but for beginners I definitely recommend it. Yes, it costs some performance, but the usual rule applies: don't optimize until you have measurements.

Share this post


Link to post
Share on other sites
[quote name='larspensjo' timestamp='1343372801' post='4963543']
In principle, you are perfectly correct. In practice, this technique helps you find cases where you are using dangling pointers.[/quote]
Not sure I buy that argument really, how would you manage to use the pointers after the destructor? It's only really relevant if you call delete on a pointer where subsequent code still has access to that pointer - such a scenario, whilst easy to concoct, should be almost always avoided. I feel it's better to abide by that rule than to bother nulling pointers after delete, it'll save you from more more bugs overall.

Share this post


Link to post
Share on other sites
[quote name='dmatter' timestamp='1343425121' post='4963771']
Not sure I buy that argument really, how would you manage to use the pointers after the destructor? It's only really relevant if you call delete on a pointer where subsequent code still has access to that pointer - such a scenario, whilst easy to concoct, should be almost always avoided.
[/quote]
I agree, of course. Maybe I wasn't clear in my description, I don't advocate it as a "design pattern". What I meant is that it helps you find bugs, while not being something you should rely on.

Share this post


Link to post
Share on other sites
[quote name='larspensjo' timestamp='1343428693' post='4963785']
[quote name='dmatter' timestamp='1343425121' post='4963771']
Not sure I buy that argument really, how would you manage to use the pointers after the destructor? It's only really relevant if you call delete on a pointer where subsequent code still has access to that pointer - such a scenario, whilst easy to concoct, should be almost always avoided.
[/quote]
I agree, of course. Maybe I wasn't clear in my description, I don't advocate it as a "design pattern". What I meant is that it helps you find bugs, while not being something you should rely on.
[/quote]
It also [i]hides[/i] bugs.

Deleting a null pointer is legal and guaranteed by the language to be a no-op. So you if you're double-deleting a pointer in some code path, you have hidden that bug and you won't discover it. At least pick a non-null pointer if you want to clear a pointer to some easy-to-spot value so that it at least is likely to crash or do something to get your attention.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement