scriggo icon indicating copy to clipboard operation
scriggo copied to clipboard

Invalid behaviour with pointers

Open zapateo opened this issue 3 years ago • 2 comments

Source code

package main

import "fmt"

var P = new(int)

func main() {
        fmt.Println(*P)
        *P = 32
        fmt.Println(*P)
        *P++
        fmt.Println(*P)
}

Scriggo output

0
32
4

gc output

0
32
33

Details

Note that this issue has been opened after closing https://github.com/open2b/scriggo/issues/916.

zapateo avatar Nov 16 '21 15:11 zapateo

A simpler version of this issue:

package main

func main() {
	p := new(int)
	*p = 1
	*p += 5
	println(*p)
}

zapateo avatar Nov 18 '21 15:11 zapateo

Note that this code:

package main

func main() {
	p := new(int)
	*p = 1
	_ = *p
	*p += 5
	println(*p)
}

works as expected. The only difference with the previous one is the addition of _ = *p.

zapateo avatar Nov 19 '21 11:11 zapateo