This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [wwwdocs] Improve example at https://gcc.gnu.org/gcc-6/porting_to.html#flifetime-dse
- From: Jonathan Wakely <jwakely at redhat dot com>
- To: Bernd Edlinger <bernd dot edlinger at hotmail dot de>
- Cc: GCC Patches <gcc-patches at gcc dot gnu dot org>, Martin Liška <mliska at suse dot cz>
- Date: Thu, 18 Aug 2016 10:01:45 +0100
- Subject: Re: [wwwdocs] Improve example at https://gcc.gnu.org/gcc-6/porting_to.html#flifetime-dse
- Authentication-results: sourceware.org; auth=none
- References: <AM4PR0701MB21626BC9965516881420AC2DE4130@AM4PR0701MB2162.eurprd07.prod.outlook.com>
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>