go: cmd/cgo: C.malloc does not support comma error return values

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go are you using (go version)?
$ go version
go version go1.6.2 windows/amd64
  1. What operating system and processor architecture are you using (go env)?
$ go env
set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\Administrator\Go
set GORACE=
set GOROOT=C:\go
set GOTOOLDIR=C:\go\pkg\tool\windows_amd64
set GO15VENDOREXPERIMENT=1
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0
set CXX=g++
set CGO_ENABLED=1
  1. What did you do?

I ran cgom.go whose source is at https://play.golang.org/p/lFkVjkjn5W

  1. What did you expect to see?

In cgo manual (https://golang.org/cmd/cgo/), the manual states that

Any C function (even void functions) may be called in a multiple assignment context to retrieve both the return value (if any) and the C errno variable as an error (use _ to skip the result value if the function returns void). For example:

I think C.malloc is C function. And Any C function may be called in a multiple assignemt context. Therefore, C.malloc should be called in a multiple assignment.

The idea was correct only if compilation would be successful.

  1. What did you see instead?
$ go run cgom.go
# command-line-arguments
.\cgom.go:22:12: no two-result form for C.malloc

Unfortunately, reality is not. C.malloc cannot be used in that way, even though free function can be used in that way.

C.malloc function seems not to be part of a set of C functions, because contraposition of “Any C function may be called in a multiple assignment context” is “if a function may not be called in a multiple context, then the function is not C function”.

This contradicts usual idea that malloc should be C function. Go override some of special functions as malloc, etc. But these functions do not act like C functions.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 21 (13 by maintainers)

Most upvoted comments

ok calm yourself I misunderstood apologies

This would be a breaking change to the API

How so? Currently the only way to call C.malloc is with zero or one return values. I don’t think anyone’s proposing changing the behavior in this case (including the panic behavior). This just adds the possibility to call it with two return values. The only oddity would be that, presumably, we would still want it to panic in the zero or one case, and not panic in the two case. This is different from other C functions, but not without precedent in the language (e.g., one- versus two-assignment type assertions).

Supporting code that previously didn’t compile, without changing behavior for code that did previously compile, doesn’t seem like a breaking change to me.

@ianlancetaylor If C.malloc changes as an exception, https://golang.org/cmd/cgo/ should be modified to specify that C.malloc is an exception.

I’m think differently. Whether C.malloc may support comma error return or not has something with consistency. There are a lot of C functions that has no defined or uesless errno such as C.malloc, C.free, … It is consistency to support comma error return regardless of advantage of that errno. I think unnecessary consistency is a consistency for consistency.