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.
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;
}
I've committed Bernd's improved example that demonstrates the new
optimization.
Thanks for everyone's input.
Index: gcc-6/porting_to.html
===================================================================
RCS file: /cvs/gcc/wwwdocs/htdocs/gcc-6/porting_to.html,v
retrieving revision 1.24
diff -u -r1.24 porting_to.html
--- gcc-6/porting_to.html 15 Aug 2016 17:32:29 -0000 1.24
+++ gcc-6/porting_to.html 18 Aug 2016 08:58:33 -0000
@@ -357,29 +357,25 @@
struct A
{
- A () {}
- void *operator new (size_t s)
- {
- void *ptr = malloc (s);
- memset (ptr, 0, s);
- return ptr;
- }
+ A() {}
- int value;
-};
+ void* operator new(size_t s)
+ {
+ void* ptr = malloc(s);
+ memset(ptr, 0xFF, s);
+ return ptr;
+ }
-A *
-__attribute__ ((noinline))
-build (void)
-{
- return new A ();
-}
+ void operator delete(void* ptr) { free(ptr); }
+
+ int value;
+};
int main()
{
- A *a = build ();
- assert (a->value == 0); /* Use of uninitialized value */
- free (a);
+ A* a = new A;
+ assert(a->value == -1); // Use of uninitialized value
+ delete a;
}
</code></pre>