Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

odin-lang

odin

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("$ ")
    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
}

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.

like image 667
tarrball Avatar asked Aug 31 '25 01:08

tarrball


1 Answers

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.

like image 155
Ryhor Spivak Avatar answered Sep 14 '25 19:09

Ryhor Spivak



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!