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

Jonathan Wakely jwakely@redhat.com
Tue Aug 16 07:12:00 GMT 2016


On 16/08/16 05:16 +0000, Bernd Edlinger wrote:
>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.

Yes, I did try allocating the same size block, setting it to something
non-zero, and freeing it again, but it was zero again when it got
reallocated (GCC is obviously too good at removing dead code :-)

>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;
>}

Thanks, I think that would be a much better example for the porting_to
page, since it demonstrates the optimization with fewer lines of code.




More information about the Gcc-patches mailing list