B
Ben Bacarisse
BartC said:[growing a string from empty, to 10 million chars]char *p;
int i,len;
p=malloc(1);
*p=0;
len=0;
for (i=1; i<=10000000; ++i) {
++len;
p=realloc(p,len+1);
p[len-1]='*';
p[len]=0;
}On three C compilers, this took about 12 seconds (on DMC, any length
of string resulted in a runtime of 0.4 seconds; a bit odd).
On my system is takes, .15 seconds
Measuring the number of times the value of p changes, with the slow
compilers, this was respectively 2335, 2336, and 2337. With DMC, it
was just 46.
and changes p 156 times. (Off-topic extra: a C++ program using s += '*'
on a std::string takes 0.07s on the same machine.)
Clearly DMC overallocates more than the others (growing allocations by
sqrt(2) apparently).
I don't see how you can conclude that. The allocated storage might be
able to grown (i.e. it is not initially over-allocated) without changing
the address.
<snip>