d传递32位数据到S
原文
从数据源传递32位数据,构造器,一切都很好,但我不能用foo()方便函数保持对象活着.
示例:
import std.stdio;
struct S
{
char[size] bytes;
alias toString this;
string toString() const
{
scope res = [ cast(string) bytes ];
return res[0];
}
}
string foo(char[size] bytes)
{
scope res = S(bytes);
return res.toString(); // 问题在此.
}
enum size = 4;
void main()
{
char[size] text = "abcd".dup;
auto m = S(text); // 测试正常
assert(m == text);
foo(text).writefln!"(%s)"; //(?)
}
//
string foo(char[size] bytes)
{
scope res = new S(bytes);
return res.toString(); // 问题在此
}
还有:我用new符号构造S时就可以了.
char[size] bytes;
alias toString this;
string toString() const
{
scope res = [ cast(string) bytes ];
return res[0];
}
这段代码是完全错误的.你错误地转换可变并限制了生命期数据为串,然后返回它.不必用域 res;该代码等价于return cast(string)bytes;,编译器为你切片,取指针,然后用新类型切片返回.
所以这是返回局部变量指针,一旦调用它的函数返回了,该变量就没了.
string foo(char[size] bytes)
{
scope res = S(bytes);
return res.toString(); // 问题在此
}
S中的那些字节在该foo函数的栈中.你返回它们的指针,然后foo函数返回,则栈上的那些字节不再存在(重用内存,但在概念上是相同的),但你保存了引用.
auto m = S(text); // test ok
assert(m == text);
这只是有效,因为m在断定点仍然存在,一旦有另一个函数,栈内存就消失了.
你或者用.idup字节,或让你的toString用void delegate(in char[])sink,然后传递字节给sink,并转换为char[].
scope res = new S(bytes);
scope关键字表明,你保证不会让该变量的引用逃逸该域.你的toString会这样做,因此编译器可能会优化掉"new";这里域和新关键字相互抵消,给你留下同样的问题.
我用new符号构造S时就可以了!
仍然是错误代码,但可在此工作,因为字节此时是GC管理而不是栈临时,所以你仍然转换为不变,但至少它不是释放后使用.
我相信当前推荐方法[1]不是使用闭包接收器,而是使toString成为接受输出区间的模板:
import std.range;
void toString(Sink)(ref Sink sink)
if (isOutputRange!(Sink, char))
{
put(sink, cast(char[]) bytes);
}
我试用outputrange但没有成功.但我是这样实现的,并且可完美地返回活串.
import std.format, std.stdio;
struct S(int len)
{
char[len] bytes;
void toString(void delegate(const(char)[]) sink) const
{
auto res = cast(char[]) bytes;
typeid(res).writeln(" toString()=>");
sink.formattedWrite("%s", res);
}
}
char[] foo(char[9] bytes)
{
auto res = cast(char[]) format("%s", S!9(bytes));
scope(exit)
typeid(res).writeln(" foo()=>");
return res;
}
void main()
{
char[9] text = " Hello D".dup;
auto test = S!9(text);
test.writeln; // ok
foo(text).writeln; // ok
}
/* Output:
char[] toString()=>
Hello D
char[] toString()=>
char[] foo()=>
Hello D
*/
浙公网安备 33010602011771号