Topic: delete nullifier


Author: joerg.barfurth@attglobal.net (Joerg Barfurth)
Date: 2000/10/11
Raw View
Valentin Bonnard <Bonnard.V@wanadoo.fr> wrote:

> David R Tribble wrote:
> > Another question I asked was, why *not* let the compiler nullify the
> > pointer?=20
>=20
> For no really good reasons really. For so few good reasons that=20
> the compiler is _already_ allowed to do that -- no change need,=20
> no DR, no nothing. You can implement and release a patch to do=20
> that in your favorite free conforming C++ compiler, and will=20
> still be conforming !

I think someone else already pointed out that it can't do that, as the
difference can be detected by a conforming program:

#include <cstring>
#include <iostream>

int main()
{
  using std::cout;

  int* p =3D new int;
  std::size_t const sz =3D sizeof(p);

  unsigned char bytes[ sz ];
  std::memcpy( bytes, &p, sz );

  // nullifying or not ?
  delete p;

  if (memcmp(bytes, &p, sz) =3D=3D 0)
     cout << "Value unchanged, Conforming.";
  else
  {
     cout << "Value changed, Nonconforming.";

     std::memcpy( bytes, &p, sz );
     p =3D 0;

     if (memcmp(bytes, &p, sz) =3D=3D 0)
        cout << " - Value was nullified.";
     else
        cout << " - Value cannot be identified.";
  }
}

Regards, J=F6rg

--=20
J=F6rg Barfurth                         joerg.barfurth@attglobal.net
-------------- using std::disclaimer; -----------------------------
Download:     StarOffice 5.2 at       http://www.sun.com/staroffice
Participate:  OpenOffice now at       http://www.OpenOffice.org

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]






Author: "Marco Dalla Gasperina" <marcodg@home.com>
Date: 2000/10/12
Raw View
"Joerg Barfurth" <joerg.barfurth@attglobal.net> wrote in message
news:1ei9nvc.ep9itunjfj4pN%joerg.barfurth@attglobal.net...

> I think someone else already pointed out that it can't do that, as the
> difference can be detected by a conforming program:

[program snipped]

The program may not work portably for a couple of reasons:

(1) the bits may be different but the values be the same
    (e.g. segment:offset representations)

(2) all 0 bits might not be NULL

That said, there is one way for a conforming implementation to
do this.  As long as the implementation knows that it is it's
own implementation of operator delete() that is being used it
can change the argument to the delete operator. (It can't
change it after the call to operator delete(), however).

I think that this goes against the spirit of the standard
because there is now an 'intimate' link between the delete
operator and operator delete() which wasn't intended.  The
evidence being that there is a definition of operator delete()
and the standard says that it can be replaced.

marco


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]
[ Note that the FAQ URL has changed!  Please update your bookmarks.     ]