Returning dynamic array from the function

timewulf timewulf at network-application.de
Tue Jan 29 09:01:36 PST 2013


On Wednesday, 17 October 2012 at 20:38:03 UTC, bearophile wrote:
> Jonathan M Davis:
>
>>there's no way for the compiler to always catch it for you.<
>
> I think there are type systems able to always catch this kind 
> of bug (conservative region analysis, it means that if it can't 
> demonstrate the memory doesn't escape, it prudently refuses the 
> code). D doesn't have such kind of type system.
>
> Bye,
> bearophile

Hi, sorry, I didn't see this thread earlier.

I'm new with the D-Language, but very interested.

My opinion is, that this behaviour mustn't be allowed with any 
modern compiler.

The solution isn't as complicated as it looks on first hand: The 
compiler will just have to test, if a return-value has got an 
address on the stack of the function, what shall just even be 
closed.

The actual code doesn't calculate any pointer addresses by hand. 
The address is on the stack, because of the declaration of a1. b1 
takes over this address and therefor there must be a flag in the 
symboltable, indicating it mustn't be used for return-value. This 
doesn't have to do anything with safe or pure. It's an error in 
any code.

Think of the following: What will happen with a2, when for 
example a2 will be given a new value? It's actual address points 
on a deprecated address on stack.

The resulting runtime errors one can just imagine.


More information about the Digitalmars-d-learn mailing list