Thread overview | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
November 22, 2014 How to pass static array to function not by value? | ||||
---|---|---|---|---|
| ||||
I tried to pass pointer to static array but it didn't work. |
November 22, 2014 Re: How to pass static array to function not by value? | ||||
---|---|---|---|---|
| ||||
Posted in reply to drug Attachments: | On Sat, 22 Nov 2014 18:20:44 +0400 drug via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > I tried to pass pointer to static array but it didn't work. i tried it right now and it works. if you really want to get some help, you'd better give us something to start with. i.e. your code, minified. D is great, but it still can't grant telepathic abilities to us. |
November 22, 2014 Re: How to pass static array to function not by value? | ||||
---|---|---|---|---|
| ||||
Posted in reply to drug | On Saturday, 22 November 2014 at 15:20:55 UTC, drug wrote:
> I tried to pass pointer to static array but it didn't work.
try this:
import std.stdio;
void change(ref int[3] arr)
{
arr[1] = 6;
}
void main()
{
int[3] a = [1, 2, 3];
writeln("a = ", a);
change(a);
writeln("a = ", a);
}
-Eric
|
November 22, 2014 Re: How to pass static array to function not by value? | ||||
---|---|---|---|---|
| ||||
Posted in reply to drug | On Saturday, 22 November 2014 at 15:20:55 UTC, drug wrote:
> I tried to pass pointer to static array but it didn't work.
Also, if you really want to be lame and actually use a pointer
try this:
import std.stdio;
void change(int *arr)
{
arr[1] = 6;
}
void main()
{
int[3] a = [1, 2, 3];
writeln("a = ", a);
change(a.ptr);
writeln("a = ", a);
}
Maybe this is not so lame because change() can take
any length of static array.
-Eric
|
November 22, 2014 Re: How to pass static array to function not by value? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Eric Attachments: | On Sat, 22 Nov 2014 15:45:51 +0000 Eric via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > Maybe this is not so lame because change() can take > any length of static array. void change (int[] arr) { arr[1] = 42; } void main () { int[$] a = [1, 2, 3]; change(a); import std.stdio : writeln; writeln(a); // [1, 42, 3] } |
November 22, 2014 Re: How to pass static array to function not by value? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | On Saturday, 22 November 2014 at 15:57:40 UTC, ketmar via Digitalmars-d-learn wrote:
> On Sat, 22 Nov 2014 15:45:51 +0000
> Eric via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote:
>
>> Maybe this is not so lame because change() can take
>> any length of static array.
>
> void change (int[] arr) {
> arr[1] = 42;
> }
>
> void main () {
> int[$] a = [1, 2, 3];
> change(a);
> import std.stdio : writeln;
> writeln(a); // [1, 42, 3]
> }
Okay, the pointer way really is lame then:)
-Eric
|
November 22, 2014 Re: How to pass static array to function not by value? | ||||
---|---|---|---|---|
| ||||
Posted in reply to ketmar | On 22.11.2014 19:34, ketmar via Digitalmars-d-learn wrote: > On Sat, 22 Nov 2014 18:20:44 +0400 > drug via Digitalmars-d-learn<digitalmars-d-learn@puremagic.com> wrote: > >> I tried to pass pointer to static array but it didn't work. > i tried it right now and it works. > > if you really want to get some help, you'd better give us something to > start with. i.e. your code, minified. D is great, but it still can't > grant telepathic abilities to us. Sorry for inconvenience. http://dpaste.dzfl.pl/64ab69ae80d2 this causes stackoverflow because static array is big enough. I'd like to pass it not by value to avoid stack overflowing. Even if I use ref dmd pass it by value. |
November 22, 2014 Re: How to pass static array to function not by value? | ||||
---|---|---|---|---|
| ||||
On Sat, Nov 22, 2014 at 05:57:30PM +0200, ketmar via Digitalmars-d-learn wrote: > On Sat, 22 Nov 2014 15:45:51 +0000 > Eric via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > > > Maybe this is not so lame because change() can take > > any length of static array. > > void change (int[] arr) { > arr[1] = 42; > } > > void main () { > int[$] a = [1, 2, 3]; > change(a); This is actually really scary that static arrays implicitly decay to a dynamic array slice. It can cause all sorts of problems with escaping dangling references to local variables: Example #1: int[] bad(int[] arr) { return arr; } int[] equallyBad() { int[3] x = [1,2,3]; return bad(x); // oops } Example #2: struct S { int[] data; this(int[] arr) { data = arr; } } S makeS() { int[3] x = [1,2,3]; return S(x); // oops } Example #3: struct S { int[] data; this(int[] arr...) { // red flag data = arr; } } void main() { auto s = S([1,2,3]); // OK auto t = S(1,2,3); // NG } IMO, at the very least, an explicit slice should be required to turn a static array into a dynamic array slice. That way, the programmer at least has to think for 1 second (hopefully more) whether or not he should be passing a slice to a static array. T -- Those who don't understand Unix are condemned to reinvent it, poorly. |
November 22, 2014 Re: How to pass static array to function not by value? | ||||
---|---|---|---|---|
| ||||
Posted in reply to drug | On 11/22/2014 07:07 AM, drug wrote: > On 22.11.2014 19:34, ketmar via Digitalmars-d-learn wrote: >> On Sat, 22 Nov 2014 18:20:44 +0400 >> drug via Digitalmars-d-learn<digitalmars-d-learn@puremagic.com> wrote: >> >>> I tried to pass pointer to static array but it didn't work. >> i tried it right now and it works. >> >> if you really want to get some help, you'd better give us something to >> start with. i.e. your code, minified. D is great, but it still can't >> grant telepathic abilities to us. > > Sorry for inconvenience. > http://dpaste.dzfl.pl/64ab69ae80d2 > this causes stackoverflow because static array is big enough. If it overflows the stack just because there are two copies on it, then it will overflow for one of them in the future. (Say, after adding another variable to main.) For example, the following program has the same problem on my system without passing any argument: import std.stdio; enum A = 65536; enum B = 50; alias MyType = int[A][B]; void main() { MyType arr; } make: *** [deneme] Segmentation fault > I'd like to pass it not by value to avoid stack overflowing. Even if I use > ref dmd pass it by value. I would dynamically allocate the storage. There is the following convenient syntax: import std.stdio; enum A = 65536; enum B = 1000; // <-- Much larger now alias MyType = int[][]; void foo(ref MyType arr) { writeln(arr[3][40]); } void main() { auto arr = new int[][](B, A); // <-- "A elements of int[B] each" foo(arr); } Ali |
November 22, 2014 Re: How to pass static array to function not by value? | ||||
---|---|---|---|---|
| ||||
Posted in reply to drug | On Saturday, 22 November 2014 at 16:07:25 UTC, drug wrote:
> On 22.11.2014 19:34, ketmar via Digitalmars-d-learn wrote:
>> On Sat, 22 Nov 2014 18:20:44 +0400
>> drug via Digitalmars-d-learn<digitalmars-d-learn@puremagic.com> wrote:
>>
>>> I tried to pass pointer to static array but it didn't work.
>> i tried it right now and it works.
>>
>> if you really want to get some help, you'd better give us something to
>> start with. i.e. your code, minified. D is great, but it still can't
>> grant telepathic abilities to us.
>
> Sorry for inconvenience.
> http://dpaste.dzfl.pl/64ab69ae80d2
> this causes stackoverflow because static array is big enough. I'd like to pass it not by value to avoid stack overflowing. Even if I use ref dmd pass it by value.
Your problem is not the reference issue. D has a limit on how big
static arrays can be in a function. You can make them bigger by declaring them
globally, but I think even that has a limit. Try this:
import std.stdio;
enum A = 65536;
enum B = 32;
alias MyType = int[A][B];
void foo(ref MyType arr)
{
writeln(arr[3][3]);
}
MyType arr;
void main()
{
writeln("arr[3][3] = ", arr[3][3]);
foo(arr);
}
-Eric
|
Copyright © 1999-2021 by the D Language Foundation