| Thread overview | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 05, 2015 how can I get a reference of array? | ||||
|---|---|---|---|---|
| ||||
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.
| ||||
February 05, 2015 Re: how can I get a reference of array? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to zhmt | void test(ref int[] arr) {
arr ~= 4;
}
It is something like ref variables. But ref just be used in
function declaration.
| |||
February 05, 2015 Re: how can I get a reference of array? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to zhmt | 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 | |||
February 05, 2015 Re: how can I get a reference of array? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | @Ali I know the direction now, I should learn more about the pointers. Thx very much. | |||
February 05, 2015 Re: how can I get a reference of array? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to 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? | |||
February 05, 2015 Re: how can I get a reference of array? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to zhmt | The behavior of array is more or less unpredictable. If it is always a reference like class , it will be more predictable. | |||
February 05, 2015 Re: how can I get a reference of array? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to zhmt | 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
| |||
February 05, 2015 Re: how can I get a reference of array? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to zhmt | 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!!! | |||
February 05, 2015 Re: how can I get a reference of array? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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.
| |||
February 05, 2015 Re: how can I get a reference of array? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | "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. | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply