Re: Gc/D_runtime prevents dangling pointers?

2018-01-04 Thread Steven Schveighoffer via Digitalmars-d-learn
On 1/4/18 6:50 AM, thedeemon wrote: On Thursday, 4 January 2018 at 11:05:25 UTC, tipdbmp wrote: It seems that the '~=' operator "knows" that there's a reference to 'a's old memory and it keeps it around instead of freeing it. It's just not its job to free that memory. That memory is freed

Re: Gc/D_runtime prevents dangling pointers?

2018-01-04 Thread thedeemon via Digitalmars-d-learn
On Thursday, 4 January 2018 at 11:05:25 UTC, tipdbmp wrote: What is your definition of a dangling pointer? A pointer pointing to freed memory, which presumably '[0]' should be because it reallocates. It allocates a larger array, but the old version is not freed up front. Right because there

Re: Gc/D_runtime prevents dangling pointers?

2018-01-04 Thread tipdbmp via Digitalmars-d-learn
What is your definition of a dangling pointer? A pointer pointing to freed memory, which presumably '[0]' should be because it reallocates. It seems that the '~=' operator "knows" that there's a reference to 'a's old memory and it keeps it around instead of freeing it. I just don't

Re: Gc/D_runtime prevents dangling pointers?

2018-01-03 Thread thedeemon via Digitalmars-d-learn
On Wednesday, 3 January 2018 at 22:22:06 UTC, tipdbmp wrote: x doesn't seem to be a dangling pointer, how come? What is your definition of a dangling pointer? In the shown example we have a reference to a piece of memory containing 'x', so this memory is not freed, it's used by the program.

Gc/D_runtime prevents dangling pointers?

2018-01-03 Thread tipdbmp via Digitalmars-d-learn
char * get_dangling_ptr() { char[] a; a.reserve(15); a ~= 'x'; char *x = [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