February 05, 2015
On Thursday, 5 February 2015 at 06:58:09 UTC, Ali Çehreli wrote:
> On 02/04/2015 10:42 PM, zhmt wrote:
>> Here is a simple code snippet:

With this approach, the allocation of `arr` itself is often clumsy and error-prone. In this case it is important to note that `arr` is allocated on the stack and thus has scoped lifetime.

Consider using std.container.array.Array instead.
February 05, 2015
On Thursday, 5 February 2015 at 10:12:47 UTC,  wrote:
> On Thursday, 5 February 2015 at 06:58:09 UTC, Ali Çehreli wrote:
>> On 02/04/2015 10:42 PM, zhmt wrote:
>>> Here is a simple code snippet:
>
> With this approach, the allocation of `arr` itself is often clumsy and error-prone. In this case it is important to note that `arr` is allocated on the stack and thus has scoped lifetime.
>
> Consider using std.container.array.Array instead.

@Jakob Ovrum

Thx for your advice.

I am still learning dlang, I will pay attention to memory management in the future.
February 05, 2015
On 2015-02-05 at 09:58, bearophile wrote:
> zhmt:
>
>> Will arr.ptr change in the future?
>>
>> As the array add more members , it need more memroy, then remalloc may be called, the pointer maybe change, then the stored pointer will be invalid.
>>
>> Will this happen?
>
> Yes, it can happen.

Therefore, don't use arr.ptr directly, but always access it via arr or a.arr.

    class A { public int[] * arr; }
    int[] arr;
    A a = new A;
    a.arr = &arr;
    ... // lots of adding to arr
    ... // and yet still, a.arr == &arr

Even when the array in the memory gets reallocated by adding to arr, causing arr.length and arr.ptr to be updated, the arr struct itself will remain in the same spot, so pointers to it, including a.arr, are valid. (Unless arr goes out of scope before a, in which case you would be in deep trouble.)
February 05, 2015
On 02/05/2015 01:49 AM, Christof Schardt wrote:
>
> "Ali Çehreli" <acehreli@yahoo.com> schrieb im Newsbeitrag
> news:mav4a1$l3a$1@digitalmars.com...
>> On 02/04/2015 10:42 PM, zhmt wrote:
>> The following article is very informative:
>>
>>   http://dlang.org/d-array-article.html
>
> Nice and important article.
>
> But: How could I have looked this up this myself?
> What should be the official path to this link?
>
> I tried "Books&Articles" and "Community => More Links"
> and could not find it.
>
>

The link used to be there. Please file a documentation bug presumably conveniently through the "Improve this page" link on dlang.org.

Thank you,
Ali

1 2
Next ›   Last »