2020 New Year Coding Puzzler — The Curse of the Pointer

--

My coding started with C and I thus know pointers inside out. But do you? I will give you a bit of code, and there is a pointer problem with it. Can you determine the bug?

The one thing I learn about pointers when I taught C, was that everything was fine, until we hit pointers and passing parameters with them, and most of the students struggled to get the concepts of them. The great thing about Golang is that we can use pointers, but also have the integration of GitHub code. If we have a program of msm.go, we compile it with:

$ go build msm.go

Here is the code in Golang, and which uses pointers for the creation of Big Integers, and which calculates x to the power of y:

package mainimport "fmt"
import "strconv"
import "os"
import "math/big"
func exp_func(x, y *big.Int) *big.Int {exp := strconv.FormatInt(y.Int64(), 2)
var value = new(big.Int)
value = x
fmt.Printf("Exp: %s \n", exp)
for i := 1; i < len(exp); i++ {
value.Mul(value,value)
fmt.Printf("%d %d (square)\n", i,value)
if(exp[i]=='1') {
value.Mul(x,value)
fmt.Printf("%d %d (multiply) x=%s\n", i,value,x)
}
}
return value
}
func main() {
argCount :=…

--

--

Prof Bill Buchanan OBE FRSE
ASecuritySite: When Bob Met Alice

Professor of Cryptography. Serial innovator. Believer in fairness, justice & freedom. Based in Edinburgh. Old World Breaker. New World Creator. Building trust.