Thread overview | ||||||
---|---|---|---|---|---|---|
|
November 06, 2008 will .length=0 clear memory | ||||
---|---|---|---|---|
| ||||
i just wonder here, free(x); will free contents of x, but will x.length=0 free memory too? |
November 06, 2008 Re: will .length=0 clear memory | ||||
---|---|---|---|---|
| ||||
Posted in reply to Big Bill | On Thu, Nov 6, 2008 at 2:02 PM, Big Bill <bigdorkyb@tengu.com> wrote:
> i just wonder here,
>
> free(x); will free contents of x, but will x.length=0 free memory too?
No it does not, and this is how you can implement a "reserve_capacity" kind of function for D arrays.
void reserve_capacity(T)(ref T[] a, size_t N) {
size_t orig_length = a.length;
if (orig_length >= N) return;
a.length = N;
a.length = orig_length;
}
|
November 06, 2008 Re: will .length=0 clear memory | ||||
---|---|---|---|---|
| ||||
Posted in reply to Big Bill | On Thu, Nov 6, 2008 at 12:02 AM, Big Bill <bigdorkyb@tengu.com> wrote:
> i just wonder here,
>
> free(x); will free contents of x, but will x.length=0 free memory too?
free(x) doesn't clear the contents of x. In fact it's completely
illegal and I'm surprised your compiler allows it. Not only can
arrays not be implicitly cast to pointers, but free() is a C stdlib
function, and using free() to free memory allocated by the D GC is a
terrible idea.
Did you mean "delete x;"?
|
November 06, 2008 Re: will .length=0 clear memory | ||||
---|---|---|---|---|
| ||||
On Thu, Nov 6, 2008 at 2:46 PM, Jarrett Billingsley <jarrett.billingsley@gmail.com> wrote: > On Thu, Nov 6, 2008 at 12:02 AM, Big Bill <bigdorkyb@tengu.com> wrote: >> i just wonder here, >> >> free(x); will free contents of x, but will x.length=0 free memory too? > > free(x) doesn't clear the contents of x. In fact it's completely > illegal and I'm surprised your compiler allows it. Not only can > arrays not be implicitly cast to pointers, but free() is a C stdlib > function, and using free() to free memory allocated by the D GC is a > terrible idea. > > Did you mean "delete x;"? And if you want to let the GC handle it, then x = null; For some reason I completely missed that free(x) in the original mail. Yeh, free() on a D array is not right. --bb |
Copyright © 1999-2021 by the D Language Foundation