Thread overview | ||||||
---|---|---|---|---|---|---|
|
June 04, 2015 What does program do when array is returned from function? | ||||
---|---|---|---|---|
| ||||
[code] char[] test(){ auto t = new char[5]; return t; } [/code] Is the test function returning just a pointer from heap or does copy operation? It is not obvious what it does, and I am trying to avoid doing this always. |
June 04, 2015 Re: What does program do when array is returned from function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to tcak | On Thu, 04 Jun 2015 07:03:30 +0000 tcak via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote: > [code] > char[] test(){ > auto t = new char[5]; > > return t; > } > [/code] > > Is the test function returning just a pointer from heap or does copy operation? > this is same as: auto t = new char[](5) it will return array(which is struct with pointer to data and size) > It is not obvious what it does, and I am trying to avoid doing this always. there is no reason. But be carefull, this could be wrong [code] char[] test(){ char[5] t; return t; } [/code] |
June 04, 2015 Re: What does program do when array is returned from function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozák | On Thursday, 4 June 2015 at 07:20:24 UTC, Daniel Kozák wrote:
>
> On Thu, 04 Jun 2015 07:03:30 +0000
> tcak via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote:
>
>> [code]
>> char[] test(){
>> auto t = new char[5];
>>
>> return t;
>> }
>> [/code]
>>
>> Is the test function returning just a pointer from heap or does copy operation?
>>
>
> this is same as:
> auto t = new char[](5)
>
> it will return array(which is struct with pointer to data and size)
>
>> It is not obvious what it does, and I am trying to avoid doing this always.
>
> there is no reason.
>
> But be carefull, this could be wrong
>
> [code]
> char[] test(){
> char[5] t;
> return t;
> }
> [/code]
Fortunately this doesn't even compile with current DMD.
|
June 04, 2015 Re: What does program do when array is returned from function? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marc Schütz | On Thursday, 4 June 2015 at 09:42:04 UTC, Marc Schütz wrote:
> On Thursday, 4 June 2015 at 07:20:24 UTC, Daniel Kozák wrote:
>>
>> On Thu, 04 Jun 2015 07:03:30 +0000
>> tcak via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote:
>>
>>> [code]
>>> char[] test(){
>>> auto t = new char[5];
>>>
>>> return t;
>>> }
>>> [/code]
>>>
>>> Is the test function returning just a pointer from heap or does copy operation?
>>>
>>
>> this is same as:
>> auto t = new char[](5)
>>
>> it will return array(which is struct with pointer to data and size)
>>
>>> It is not obvious what it does, and I am trying to avoid doing this always.
>>
>> there is no reason.
>>
>> But be carefull, this could be wrong
>>
>> [code]
>> char[] test(){
>> char[5] t;
>> return t;
>> }
>> [/code]
>
> Fortunately this doesn't even compile with current DMD.
Yes. Trying to return a variable from stack doesn't sound like a good idea other than copying its value somewhere else.
|
Copyright © 1999-2021 by the D Language Foundation