On Tue, Sep 23, 2014 at 01:30:41PM +0100, Dave Mitchell wrote: > On Tue, Sep 23, 2014 at 12:49:56PM +0100, Dave Mitchell wrote: > > For example the following simple C code: > > > > #include <stdio.h> > > #include <malloc.h> > > > > int main(int argc, char**argv) > > { > > int i; > > void *q, *p = malloc(1); > > malloc(1); /* poison reallocs */ > > for (i=1; i<130; i++) { > > q = realloc(p,i); > > if (p != q) { > > printf("after %3d bytes realloc() using different address\n", i-1); > > malloc(i); /* poison reallocs */ > > } > > p=q; > > } > > } > > > > gives this output on my system: > > > > after 24 bytes realloc() using different address > > after 40 bytes realloc() using different address > > after 56 bytes realloc() using different address > > after 72 bytes realloc() using different address > > after 88 bytes realloc() using different address > > after 104 bytes realloc() using different address > > after 120 bytes realloc() using different address > > PS > > I'd be interested in what people get with that code on other OSes. Solaris 11 / i386 : after 16 bytes realloc() using different address after 24 bytes realloc() using different address after 32 bytes realloc() using different address after 40 bytes realloc() using different address after 48 bytes realloc() using different address after 56 bytes realloc() using different address after 64 bytes realloc() using different address after 72 bytes realloc() using different address after 80 bytes realloc() using different address after 88 bytes realloc() using different address after 96 bytes realloc() using different address after 104 bytes realloc() using different address after 112 bytes realloc() using different address after 120 bytes realloc() using different address after 128 bytes realloc() using different address However, gcc/Linux can do odd things under optimization: Debian GNU/Linux 32-bit/x686/gcc-4.9.1 gives no optimization: after 12 bytes realloc() using different address after 20 bytes realloc() using different address after 28 bytes realloc() using different address after 36 bytes realloc() using different address after 44 bytes realloc() using different address after 52 bytes realloc() using different address after 60 bytes realloc() using different address after 68 bytes realloc() using different address after 76 bytes realloc() using different address after 84 bytes realloc() using different address after 92 bytes realloc() using different address after 100 bytes realloc() using different address after 108 bytes realloc() using different address after 116 bytes realloc() using different address after 124 bytes realloc() using different address With optimization (gcc -O) (and tweaking the program to count higher): after 135148 bytes realloc() using different address after 135156 bytes realloc() using different address after 139252 bytes realloc() using different address after 282612 bytes realloc() using different address after 569332 bytes realloc() using different address after 1142772 bytes realloc() using different address This particular optimization can be disabled with -fno-builtin. -- Andy Dougherty doughera@lafayette.eduThread Previous | Thread Next