digitalmars.D.learn - how can I get a reference of array?
- zhmt (31/31) Feb 04 2015 Here is a simple code snippet:
- zhmt (5/5) Feb 04 2015 void test(ref int[] arr) {
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (37/67) Feb 04 2015 First, in order to get what you want, add 4 starts and 2 ampersands so
- zhmt (3/3) Feb 04 2015 @Ali
- zhmt (5/5) Feb 05 2015 Will arr.ptr change in the future?
- zhmt (3/3) Feb 05 2015 The behavior of array is more or less unpredictable.
- zhmt (4/4) Feb 05 2015 Sorry, I misunderstand the meaning of array pointer, it is not
- bearophile (4/9) Feb 05 2015 Yes, it can happen.
- Christof Schardt (7/10) Feb 05 2015 Nice and important article.
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (5/16) Feb 05 2015 The link used to be there. Please file a documentation bug presumably
- Jakob Ovrum (5/7) Feb 05 2015 With this approach, the allocation of `arr` itself is often
- zhmt (5/13) Feb 05 2015 @Jakob Ovrum
Here is a simple code snippet: class A { public int[] arr; } class B { public int[] arr; } void main() { int[] arr; A a = new A; B b = new B; a.arr = arr; b.arr = arr; arr ~= 1; arr ~= -2; foreach(data; a.arr) { writeln(data); } foreach(data; b.arr) { writeln(data); } } it prints nothing, I know that a.arr and b.arr are all slices of arr. But if a and b want to reference the global arr, it means that any changes in arr will be seen by a and b, and vice versa. How to? Thanks ahead.
Feb 04 2015
void test(ref int[] arr) { arr ~= 4; } It is something like ref variables. But ref just be used in function declaration.
Feb 04 2015
On 02/04/2015 10:42 PM, zhmt wrote:Here is a simple code snippet: class A { public int[] arr; } class B { public int[] arr; } void main() { int[] arr; A a = new A; B b = new B; a.arr = arr; b.arr = arr; arr ~= 1; arr ~= -2; foreach(data; a.arr) { writeln(data); } foreach(data; b.arr) { writeln(data); } } it prints nothing, I know that a.arr and b.arr are all slices of arr. But if a and b want to reference the global arr, it means that any changes in arr will be seen by a and b, and vice versa. How to? Thanks ahead.First, in order to get what you want, add 4 starts and 2 ampersands so that there is one array and two pointers to it: import std.stdio; class A { public int[] * arr; // <-- * } class B { public int[] * arr; // <-- * } void main() { int[] arr; A a = new A; B b = new B; a.arr = &arr; // <-- & b.arr = &arr; // <-- & arr ~= 1; arr ~= -2; foreach(data; *a.arr) // <-- * { writeln(data); } foreach(data; *b.arr) // <-- * { writeln(data); } } The output: 1 -2 1 -2 Appending to a slice breaks its sharing relationship with other slices. The following article is very informative: http://dlang.org/d-array-article.html Ali
Feb 04 2015
Ali I know the direction now, I should learn more about the pointers. Thx very much.
Feb 04 2015
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?
Feb 05 2015
The behavior of array is more or less unpredictable. If it is always a reference like class , it will be more predictable.
Feb 05 2015
Sorry, I misunderstand the meaning of array pointer, it is not equals to arr.ptr. The array pointer meets my need perfectly . Ignore my replies above, Sorry!!!
Feb 05 2015
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. Bye, bearophile
Feb 05 2015
On Thursday, 5 February 2015 at 08:58:47 UTC, bearophile wrote:zhmt:Thx. I understand the difference between array pointer and arr.ptr, they are not the same thing.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. Bye, bearophile
Feb 05 2015
On 2015-02-05 at 09:58, bearophile wrote:zhmt: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.)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.
Feb 05 2015
"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.htmlNice 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.
Feb 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...The link used to be there. Please file a documentation bug presumably conveniently through the "Improve this page" link on dlang.org. Thank you, AliOn 02/04/2015 10:42 PM, zhmt wrote: The following article is very informative: http://dlang.org/d-array-article.htmlNice 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.
Feb 05 2015
On Thursday, 5 February 2015 at 06:58:09 UTC, Ali Çehreli wrote:On 02/04/2015 10:42 PM, zhmt wrote: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.Here is a simple code snippet:
Feb 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:Jakob Ovrum Thx for your advice. I am still learning dlang, I will pay attention to memory management in the future.On 02/04/2015 10:42 PM, zhmt wrote: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.Here is a simple code snippet:
Feb 05 2015