为什么这个字符串在过程返回后变为空白(Odin)?

huangapple go评论76阅读模式
英文:

Why is this string blank after the procedure returns it (Odin)?

问题

在以下的小示例程序中,从 stdin 读取输入。在 read_input 函数中,打印字符串值有效,但在 main 函数中不起作用。令人惊讶的是,在 main 函数中可以输出字符串的长度 (len),但字符串本身却是空的。

main :: proc() {
	fmt.print("$ ")
	input := read_input()

    // This will output the proper length
	fmt.println("returned string length:", len(input))

    // This will be empty
	fmt.println("returned string: ", input)
}

read_input :: proc() -> string {
	buf: [256]byte
	num_bytes, err := os.read(os.stdin, buf[:])

	if err < 0 {
		return "Error reading from stdin"
	}

	str := string(buf[:num_bytes - 1])

    // this will output the proper number of bytes
	fmt.println("bytes read: ", num_bytes)

    // this will output the string entered, minus the newline
	fmt.println("read string: ", str)

	return str
}

示例运行/输出:

$ hello
bytes read:  6
read string:  hello
returned string length: 5
returned string: 

我发现返回 strings.clone(str) 可行,但我并没有真正理解上面的问题。

英文:

In the following small sample program, input is read from stdin. Printing the string value works inside read_input, but not in main. Surprisingly, the length (len) of the string does output in main, though the string output is blank.

main :: proc() {
	fmt.print(&quot;$ &quot;)
	input := read_input()

    // This will output the proper length
	fmt.println(&quot;returned string length:&quot;, len(input))

    // This will be empty
	fmt.println(&quot;returned string: &quot;, input)
}

read_input :: proc() -&gt; string {
	buf: [256]byte
	num_bytes, err := os.read(os.stdin, buf[:])

	if err &lt; 0 {
		return &quot;Error reading from stdin&quot;
	}

	str := string(buf[:num_bytes - 1])

    // this will output the proper number of bytes
	fmt.println(&quot;bytes read: &quot;, num_bytes)

    // this will output the string entered, minus the newline
	fmt.println(&quot;read string: &quot;, str)

	return str
}

Example run/output:

$ hello
bytes read:  6
read string:  hello
returned string length: 5
returned string: 

I've discovered that returning strings.clone(str) works but I'm not really understanding the problem above.

答案1

得分: 1

在Odin中,string只是对底层数据缓冲区的一个视图。在您的情况下,它指向buf,而buf是局部于read_input过程的。一旦read_input返回,它的堆栈内存就消失了,但返回的字符串仍然指向它。

strings.clone(str)之所以起作用,是因为它为字符串复制分配了内存,并返回指向这个内存的字符串。在这种情况下,您需要在main函数中进行delete操作,以避免内存泄漏。

英文:

In Odin string is just a view over underlaying data buffer. In your case it points to buf which is local to read_input procedure. Once read_input returns its stack memory is gone, but returned string still points to it.

strings.clone(str) works because it allocates memory for a string copy and return string which points into this memory. In this case you'll need to delete it in main to avoid memory leak.

huangapple
  • 本文由 发表于 2023年7月23日 22:57:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76748903.html
  • odin
  • odin-lang
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定