This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Is gcc allowed to eliminate writes in a destructor?
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: Norbert Lange <lange at chello dot at>
- Cc: gcc-help <gcc-help at gcc dot gnu dot org>
- Date: Wed, 20 May 2015 10:42:41 +0100
- Subject: Re: Is gcc allowed to eliminate writes in a destructor?
- Authentication-results: sourceware.org; auth=none
- References: <CADYdroP2mCaE0Mrv7r4MGJLd7MPhjq-A3O+vOSqDz=vf021-yg at mail dot gmail dot com>
On 20 May 2015 at 09:27, Norbert Lange wrote:
> I did not check the resulting assembly, but to me it seems gcc removes
> anything thats not externally visibly in the destructor.
> Which is kinda ironic because I understood its more or less
> recommended at my employers to wipe your data in a destructor, helps
> in debugging but its also considered defensive programming so the
> object cant be misinterpreted as being "alive and valid".
There is no way for a valid C++ program to tell whether the data was
wiped in the destructor, because it is undefined behaviour to inspect
the members of an object after its destructor runs, so the compiler is
allowed to eliminate the writes. A correct program will not notice any
difference whether the writes happen or not.