May 05, 2005
Then, how are arrays returned? Is't not safe to return a char[] from a
function if it's allocated on the stack?!?
I must have been messed up really good then, because as a GC'ed language I
assumed one was free to return a string (char[]) from a function. I *think*
I even already did it, but I may be mistaken (or I was just very luck that
the stack didn't get corrupted)...




"Walter" <newshound@digitalmars.com> wrote in message news:d5bc2t$2pca$1@digitaldaemon.com...
>
> "Maxime Larose" <mlarose@broadsoft.com> wrote in message news:d5b9dl$2n39$1@digitaldaemon.com...
> > This will create an arrray in the heap, not on the stack. (That's a main
> > difference between C(++) and D)
>
> No, it'll be on the stack just like in C/C++.
>
>


May 05, 2005
Maxime Larose wrote:
> Then, how are arrays returned? Is't not safe to return a char[] from a
> function if it's allocated on the stack?!?
> I must have been messed up really good then, because as a GC'ed language I
> assumed one was free to return a string (char[]) from a function. I *think*
> I even already did it, but I may be mistaken (or I was just very luck that
> the stack didn't get corrupted)...

Dynamic arrays (such as char[]) are on the heap, so may be freely returned.  I assume that if one returns a static array (such as char[256]) then a copy would be made.  Of course, you could define a function as returning a dynamic array, and still return the value of a static array.  As I understand it, its data is copied to a new dynamic array, and a referance to the anonymous dynamic array is returned.

-- Chris Sauls
May 05, 2005
"Chris Sauls" <ibisbasenji@gmail.com> wrote in message news:d5csgj$sha$1@digitaldaemon.com...
> Maxime Larose wrote:
> > Then, how are arrays returned? Is't not safe to return a char[] from a
> > function if it's allocated on the stack?!?
> > I must have been messed up really good then, because as a GC'ed language
I
> > assumed one was free to return a string (char[]) from a function. I
*think*
> > I even already did it, but I may be mistaken (or I was just very luck
that
> > the stack didn't get corrupted)...
>
> Dynamic arrays (such as char[]) are on the heap, so may be freely returned.  I assume that if one returns a static array (such as char[256]) then a copy would be made.  Of course, you could define a function as returning a dynamic array, and still return the value of a static array.  As I understand it, its data is copied to a new dynamic array, and a referance to the anonymous dynamic array is returned.
>
> -- Chris Sauls


Hmm.... The behavior for static arrays seems fairly innefficient...  And
what about:
classA [1000] array;

Then I guess the classes themselves are on the heap, but the array is not? And if the array is returned, then it is copied to the heap. Or does the compiler detects this case and immediately allocate the array on the heap? What's the best way to allocate a fixed-size array directly on the heap?

Is this documented anywhere? I read the docs from cover to cover but it seems I missed a few things... I checked again in the sections that seem relevant and didn't find any of this mentionned.




May 05, 2005
Chris Sauls wrote:

> Maxime Larose wrote:
>> Then, how are arrays returned? Is't not safe to return a char[] from a
>> function if it's allocated on the stack?!?
>> I must have been messed up really good then, because as a GC'ed language
>> I assumed one was free to return a string (char[]) from a function. I
>> *think* I even already did it, but I may be mistaken (or I was just very
>> luck that the stack didn't get corrupted)...
> 
> Dynamic arrays (such as char[]) are on the heap, so may be freely
> returned.  I assume that if one returns a static array (such as
> char[256]) then a copy would be made.
Function can't return static arrays, only dynamic.
Look at the following:

int[] test()
{
        int[10] x;
        x[0] = 10;
        return x;  // Converting int[10] to int[] means getting reference of int,
                   // so this function returns reference to array on the stack
                   // which is error.
}

int main()
{
        int[] u;
        u = test();
        writefln(u[0]); // prints 10
        int[100] g;
        writefln(u[0]); // prints garbage
}

May be compiler should produce a warning here.

> Of course, you could define a
> function as returning a dynamic array, and still return the value of a
> static array.  As I understand it, its data is copied to a new dynamic
> array, and a referance to the anonymous dynamic array is returned.
> 
> -- Chris Sauls

-- 
          Vladimir
May 05, 2005
Maxime Larose wrote:

> 
> "Chris Sauls" <ibisbasenji@gmail.com> wrote in message news:d5csgj$sha$1@digitaldaemon.com...
>> Maxime Larose wrote:
>> > Then, how are arrays returned? Is't not safe to return a char[] from a
>> > function if it's allocated on the stack?!?
>> > I must have been messed up really good then, because as a GC'ed
>> > language
> I
>> > assumed one was free to return a string (char[]) from a function. I
> *think*
>> > I even already did it, but I may be mistaken (or I was just very luck
> that
>> > the stack didn't get corrupted)...
>>
>> Dynamic arrays (such as char[]) are on the heap, so may be freely returned.  I assume that if one returns a static array (such as char[256]) then a copy would be made.  Of course, you could define a function as returning a dynamic array, and still return the value of a static array.  As I understand it, its data is copied to a new dynamic array, and a referance to the anonymous dynamic array is returned.
>>
>> -- Chris Sauls
> 
> 
> Hmm.... The behavior for static arrays seems fairly innefficient...  And
> what about:
> classA [1000] array;
> 
> Then I guess the classes themselves are on the heap, but the array is not?
Yes. Array just contains references to classes.

> And if the array is returned, then it is copied to the heap.
Array can't be returned, only reference i.e. dynamic array.
So actually reference to stack is return, and your program will crash.

> Or does the compiler detects this case and immediately allocate the array on the heap?
Fixed-size arrays defined inside a function are always on stack.

> What's the best way to allocate a fixed-size array directly on the heap?
int[] array = new int[1000];

> 
> Is this documented anywhere? I read the docs from cover to cover but it seems I missed a few things... I checked again in the sections that seem relevant and didn't find any of this mentionned.

-- 
          Vladimir
May 05, 2005
Chris Sauls wrote:
> Dynamic arrays (such as char[]) are on the heap, so may be freely returned.  I assume that if one returns a static array (such as char[256]) then a copy would be made.  Of course, you could define a function as returning a dynamic array, and still return the value of a static array.  As I understand it, its data is copied to a new dynamic array, and a referance to the anonymous dynamic array is returned.

This is not correct.  If you assign a static array to a dynamic array variable, then you get a dynamic array which points to the same memory as the static array.  However, you can dup the array:

  char[1000] static_array;
  return static_array.dup;

'dup' copies the array to a new location.  This, of course, must be on the heap.
May 05, 2005
Vladimir wrote:

> int[] test()
> {
>         int[10] x;
>         x[0] = 10;
>         return x;  // Converting int[10] to int[] means getting reference of int,
>                    // so this function returns reference to array on the stack
>                    // which is error.
> }
> 
> int main()
> {
>         int[] u;
>         u = test();
>         writefln(u[0]); // prints 10
>         int[100] g;
>         writefln(u[0]); // prints garbage
> }
> 
> May be compiler should produce a warning here.

That's insane.

The compiler shouldn't issue a warning. And it shouldn't cause the program to crash. Or to print garbage.

The compiler should be able to detect when a function returns a static array. In such cases, it should allocate the array on the heap instead of the stack.

--BenjiSmith
May 05, 2005
"Benji Smith" <dlanguage@xxagg.com> wrote in message news:d5dls4$1jat$2@digitaldaemon.com...
> Vladimir wrote:
>
>> int[] test()
>> {
>>         int[10] x;
>>         x[0] = 10;
>>         return x;  // Converting int[10] to int[] means getting reference
>> of int,
>>                    // so this function returns reference to array on the
>> stack
>>                    // which is error.
>> }
>>
>> int main()
>> {
>>         int[] u;
>>         u = test();
>>         writefln(u[0]); // prints 10
>>         int[100] g;
>>         writefln(u[0]); // prints garbage
>> }
>>
>> May be compiler should produce a warning here.
>
> That's insane.
>
> The compiler shouldn't issue a warning. And it shouldn't cause the program to crash. Or to print garbage.
>
> The compiler should be able to detect when a function returns a static array. In such cases, it should allocate the array on the heap instead of the stack.
>
> --BenjiSmith

The D spec http://www.digitalmars.com/d/function.html section Local Variables says its illegal to return the address of a local variable - which presumably should be expanded to include addresses of local static arrays. None of the errors in the Local Variables section are enforced by dmd.


May 05, 2005
Ben Hinkle wrote:
> The D spec http://www.digitalmars.com/d/function.html section Local Variables says its illegal to return the address of a local variable - which presumably should be expanded to include addresses of local static arrays. None of the errors in the Local Variables section are enforced by dmd. 

So this code should produce an error?

char[] getMyString() {
  char[6] myString = "string";
  return myString;
}

I repeat: that's insane.

--BenjiSmith
May 05, 2005
Hmmm..... No wonder I was messed up. All of this is *extremely* error-prone.
It should be either:
1) all arrays are allocated on the heap; or
2) compiler detects return of static array and issues an _error_ (and not a
warning); or
3) allocation of static array to dynamic automatically copies to the heap
(this is an inferior solution IMO because hidden things are happening under
the hood)

It suits my purposes just fine in this case (I want speed over (almost)
anything else), but honestly, as Benji puts it, it is insane...

Max



"Ben Hinkle" <bhinkle@mathworks.com> wrote in message news:d5dmil$1kaa$1@digitaldaemon.com...
>
> "Benji Smith" <dlanguage@xxagg.com> wrote in message news:d5dls4$1jat$2@digitaldaemon.com...
> > Vladimir wrote:
> >
> >> int[] test()
> >> {
> >>         int[10] x;
> >>         x[0] = 10;
> >>         return x;  // Converting int[10] to int[] means getting
reference
> >> of int,
> >>                    // so this function returns reference to array on
the
> >> stack
> >>                    // which is error.
> >> }
> >>
> >> int main()
> >> {
> >>         int[] u;
> >>         u = test();
> >>         writefln(u[0]); // prints 10
> >>         int[100] g;
> >>         writefln(u[0]); // prints garbage
> >> }
> >>
> >> May be compiler should produce a warning here.
> >
> > That's insane.
> >
> > The compiler shouldn't issue a warning. And it shouldn't cause the
program
> > to crash. Or to print garbage.
> >
> > The compiler should be able to detect when a function returns a static array. In such cases, it should allocate the array on the heap instead
of
> > the stack.
> >
> > --BenjiSmith
>
> The D spec http://www.digitalmars.com/d/function.html section Local Variables says its illegal to return the address of a local variable -
which
> presumably should be expanded to include addresses of local static arrays. None of the errors in the Local Variables section are enforced by dmd.
>
>