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

Jonathan Wakely jwakely@redhat.com
Mon Aug 15 17:43:00 GMT 2016

On 15/08/16 18:31 +0100, Jonathan Wakely wrote:
>In a later patch I'd also like to simplify the example to remove the
>build(void) function and just do A* a = new A; in main(), because the
>build function doesn't seem to serve any purpose. The assertion in the
>example doesn't fail, so it doesn't demonstrate the problem, and so
>the attribute((noinline)) is just noise. Am I missing something?
>I'd also like to replace the GNU coding style with more idiomatic C++.
>This is meant to be documentation for end-users writing C++, who don't
>typically put spaces before the argument list of a function, or use
>(void) for functions with no arguments. Writing (void) is redundant in
>C++, and even sometimes considered an abomination.

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

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

struct A
  A() {}

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

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

  int value;

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

More information about the Gcc-patches mailing list