Gc/D_runtime prevents dangling pointers?
tipdbmp
email at example.com
Wed Jan 3 22:22:06 UTC 2018
char * get_dangling_ptr() {
char[] a;
a.reserve(15);
a ~= 'x';
char *x = &a[0];
auto a_initial_ptr = a.ptr;
foreach (_; 0 .. 30) {
a ~= 'y';
//a.assumeSafeAppend() ~= 'y';
}
assert(a.ptr != a_initial_ptr, "a should've reallocated");
// trying to reuse 'a's old memory
foreach (_; 0 .. 10) {
char[] b;
b.reserve(15);
foreach (__; 0 .. 15) {
b ~= 'y';
}
}
return x;
}
void main() {
import std.stdio : writefln;
char *x = get_dangling_ptr();
writefln("ptr: %X; value: %s", x, *x);
}
x doesn't seem to be a dangling pointer, how come?
More information about the Digitalmars-d-learn
mailing list