Re: Append to dynamic array that was allocated via calloc

2017-07-25 Thread Steven Schveighoffer via Digitalmars-d-learn
On 7/25/17 8:40 AM, John Burton wrote: I can create a "slice" using non-gc allocated memory. int* ptr = cast(int*)calloc(int.sizeof, 10); int[] data = ptr[0..10]; If I don't want a memory leak I have to call free(ptr) somewhere as it won't be GC collected when data or ptr go

Re: Append to dynamic array that was allocated via calloc

2017-07-25 Thread John Burton via Digitalmars-d-learn
On Tuesday, 25 July 2017 at 13:24:36 UTC, Mike Parker wrote: On Tuesday, 25 July 2017 at 12:40:13 UTC, John Burton wrote: [...] This should give you the answer: writefln("Before: ptr = %s capacity = %s", slice.ptr, slice.capacity); slice ~= 1; writefln("After: ptr = %s capacity = %s", slice

Re: Append to dynamic array that was allocated via calloc

2017-07-25 Thread Mike Parker via Digitalmars-d-learn
On Tuesday, 25 July 2017 at 12:40:13 UTC, John Burton wrote: I can create a "slice" using non-gc allocated memory. int* ptr = cast(int*)calloc(int.sizeof, 10); int[] data = ptr[0..10]; If I don't want a memory leak I have to call free(ptr) somewhere as it won't be GC collected

Re: Append to dynamic array that was allocated via calloc

2017-07-25 Thread Dukc via Digitalmars-d-learn
On Tuesday, 25 July 2017 at 12:40:13 UTC, John Burton wrote: What happens? It seems to successfully append an extra value to the array. It appears to "work" when I try it in my compiler but I don't understand how. Will this be trying to write beyond the memory I calloc'ed? The language makes

Append to dynamic array that was allocated via calloc

2017-07-25 Thread John Burton via Digitalmars-d-learn
I can create a "slice" using non-gc allocated memory. int* ptr = cast(int*)calloc(int.sizeof, 10); int[] data = ptr[0..10]; If I don't want a memory leak I have to call free(ptr) somewhere as it won't be GC collected when data or ptr go out of scope. I presume there is nothing