[wwwdocs] Improve example at https://gcc.gnu.org/gcc-6/porting_to.html#flifetime-dse

Bernd Edlinger bernd.edlinger@hotmail.de
Tue Aug 16 05:18:00 GMT 2016


Hi Jonathan,

> I think this would be an improvement, although I still can't get the
> assertion to fail:

Probably because the memory is still initialized to zero,
when it is used for the first time.

Try this:

#include <stdlib.h>
#include <string.h>
#include <assert.h>

struct A
{
 A() {}

 void* operator new(size_t s)
 {
   void* ptr = malloc(s);
   memset(ptr, 0xFF, s);
   return ptr;
 }

 void operator delete(void* ptr) { free(ptr); }

 int value;
};

int main()
{
 A* a =  new A;
 assert(a->value == -1); /* Use of uninitialized value */
 delete a;
}



Bernd.


More information about the Gcc-patches mailing list