Can you find out where the code goes wrong?

davidl davidl at nospam.org
Sun May 24 20:47:19 PDT 2009


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.



More information about the Digitalmars-d mailing list