You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
bcmills:~/src$ go version
go version devel +12f3099c7d Sat May 13 00:09:49 2017 -0400 linux/amd64
bcmills:~/src$ $(go env CC) --version
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
My use-case is for checking conversions between Go types (such as int) and corresponding C types (such as C.size_t). I'm attempting to define a portable constant for SIZE_MAX:
const SizeMax = uint64(C.SIZE_MAX)
But that gives me a compile error. A simple whole program demonstrating the problem:
package main
/*#include <limits.h>#include <stdint.h>*/import"C"import"fmt"constSizeMax=uint64(C.SIZE_MAX)
funcmain() {
fmt.Println(SizeMax)
}
with output:
bcmills:~/src$ go run sizemax/sizemax.go
# command-line-arguments
sizemax/sizemax.go:11[sizemax.cgo1.go:12:23]: constant -1 overflows uint64
A larger example program shows that the C constant is defined in C as a large unsigned number, but cgo is compiling it to the two's-complement signed constant:
package main
/*#include <limits.h>#include <stdint.h>#define xstr(s) str(s)#define str(s) #s// size_max_str returns a C string containing the definition of SIZE_MAX.static const char* size_max_str() { return xstr(SIZE_MAX);}*/import"C"import"fmt"funcmain() {
fmt.Println(C.GoString(C.size_max_str()))
fmt.Println(C.SIZE_MAX)
}
Actual output:
bcmills:~/src$ go run sizemax/sizemax.go
(18446744073709551615UL)
-1
Expected:
Compile error, because the constant 18446744073709551615 overflows the default int type for integer constants.
My use-case is for checking conversions between Go types (such as
int
) and corresponding C types (such asC.size_t
). I'm attempting to define a portable constant for SIZE_MAX:But that gives me a compile error. A simple whole program demonstrating the problem:
with output:
A larger example program shows that the C constant is defined in C as a large unsigned number, but cgo is compiling it to the two's-complement signed constant:
Actual output:
Expected:
Compile error, because the constant 18446744073709551615 overflows the default
int
type for integer constants.(CC @ianlancetaylor @mdempsky; see also #4120 and #2470.)
The text was updated successfully, but these errors were encountered: