Re: STL Map Scoping Issue
Markus Schoder wrote:
:: On Sun, 12 Aug 2007 09:38:40 +0200, Frank Birbacher wrote:
::: sfncook@gmail.com schrieb:
:::: delete shipTypeMap[0];
:::
::: At least you have undefined behaviour here: deleted pointers are
::: invalid. invalid pointers may not be read or copied, but only be
::: assigned to or destructed (when going out of scope). But elements
::: of a map must be copyable. So you should first remove the pointer
::: from the map (store it in a local variable) and then delete the
::: object it points to.
::
:: Must say I did not realise that until now. That is unbelievably
:: ugly.
But that is also the way some hardware work(ed). Loading a pointer
into an address register might involve verifying access rights and/or
validity.
On a segmented system, like Windows 286 with 64 kB per segment,
deleting an entire segment might make the runtime release the segment
to to OS, which promptly removed the segment from the descriptor
table. Now, loading that pointer into a segment register would case a
missing segment trap.
This segment hardware is still present in 32 bit x86 processors,
though we don't use it much. The language rules are there to allow its
use, if anybody should want to!
::
:: Using the following should be safe though:
::
:: template<class T> void deleteAndZero(T *&p)
:: {
:: delete p;
:: p = NULL;
:: }
Yes.
Bo Persson