Can you find out where the code goes wrong?
Steven Schveighoffer
schveiguy at yahoo.com
Tue May 26 06:58:33 PDT 2009
On Sun, 24 May 2009 23:47:19 -0400, davidl <davidl at nospam.org> wrote:
> import std.stdio;
>
> string func()
> {
> string s="abc";
> return s;
> }
>
> void func1()
> {
> writefln("func1");
> string v = func();
> writefln("call func");
> writefln(func2());
> }
>
> byte[] func2()
> {
> writefln("hello!");
> byte[16] v= [65,65,65,65,
> 65,65,65,65,
> 65,65,65,65,
> 65,65,65,65];
> writefln(v[0..16]);
> return v[0..16];
> }
>
> void main(string[] args)
> {
> func1();
> }
>
> The culprit is the on stack array.
>
> Should the compiler warn on slicing on a fixed length array? or even
> give an error?
> I find this use case can easily go wrong! You may even think this code
> is correct at the very first glance.
This can't be detected at compile time without full escape analysis.
It is an issue that has been in D forever. And they are hard to find, so
it would be nice if it were flagged by the compiler, but I don't think
it's going to happen anytime soon.
-Steve
More information about the Digitalmars-d
mailing list