www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - how can I get a reference of array?

reply "zhmt" <zhmtzhmt qq.com> writes:
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
next sibling parent "zhmt" <zhmtzhmt qq.com> writes:
void test(ref int[] arr) {
	arr ~= 4;
}

It is something like ref variables. But ref just be used in
function declaration.
Feb 04 2015
prev sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
next sibling parent reply "zhmt" <zhmtzhmt qq.com> writes:
 Ali

I know the direction now, I should learn more about the pointers. 
Thx very much.
Feb 04 2015
parent reply "zhmt" <zhmtzhmt qq.com> writes:
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
next sibling parent reply "zhmt" <zhmtzhmt qq.com> writes:
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
parent "zhmt" <zhmtzhmt qq.com> writes:
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
prev sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
next sibling parent "zhmt" <zhmtzhmt qq.com> writes:
On Thursday, 5 February 2015 at 08:58:47 UTC, 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. Bye, bearophile
Thx. I understand the difference between array pointer and arr.ptr, they are not the same thing.
Feb 05 2015
prev sibling parent FG <home fgda.pl> writes:
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.)
Feb 05 2015
prev sibling next sibling parent reply "Christof Schardt" <Christof Schardt.info> writes:
"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.
Feb 05 2015
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Feb 05 2015
prev sibling parent reply "Jakob Ovrum" <jakobovrum gmail.com> writes:
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.
Feb 05 2015
parent "zhmt" <zhmtzhmt qq.com> writes:
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.
Feb 05 2015