Error: functions cannot return static array float[4u][4u]

Spacen Jasset spacen at yahoo.co.uk
Thu Feb 21 16:23:45 PST 2008


Spacen Jasset wrote:
> Jason House wrote:
>> Jason House wrote:
>>
>>> Spacen Jasset wrote:
>>>
>>>> For this member function:
>>>>
>>>> float[4][4] toFloatArray4x4()
>>>> {
>>>> float f[4][4];
>>>> return f;
>>>> }
>>> This looks like doing the following in C++:
>>> C toC(){
>>>   C c;
>>>   return c;
>>> }
>>>
>>> Which is an error (regardless of what the compiler says).  c is 
>>> allocated
>>> on
>>> the heap and is deallocated when the function exits.  It may be that the
>>> same effect is happening here.  I'm sure this kind of thing could get
>>> fixed in a similar way to how full closures were fixed...
>>
>> I don't know why I said heap instead of stack, but I should have said 
>> stack.
> Hmm. That's legal in both C and C++ and works so long as 'C' isn't a 
> pointer or array (otherwise you get a leak). I suppose then, that you 
> could argue it's the same in D given that I was trying to return an array.
> 
> On the other hand I observe that D arrays do not decompose into pointers 
> like the do in C and C++ and so I guess I was expecting that returning 
> an array would be possible since its more or less a proper 'object' 
> unlike in C or C++. Returning a static array could then work in exactly 
> the same way as returning a struct in D since they are fixed size and 
> static in nature.
> 
> Anyway, it seems it's not possible, and so I wonder what (in general) 
> would be the way to go, return a float[][] object, or use a ( ref 
> float[][] ) to pass the value back out, any suggestions?
I have just realized that arrays are symantically pass by reference 
always whereas structs are value types, and so that is why you cannot 
return a static array.

Another solution to my problem is of course to use a Boxer for a 
float[16] and return that I guess.


More information about the Digitalmars-d-learn mailing list