英文:
When I write an int64 type number to the function, a different number is returned
问题
我将golang代码转换为C代码,并从Python中调用它。但是当函数应该返回一个接近我输入的数字时,它返回一个非常不同的数字。
main.py
import ctypes
library = ctypes.cdll.LoadLibrary('./maintain.so')
hello_world = library.helloWorld
numb = 5000000000
n = ctypes.c_int64(numb)
x = hello_world(n)
print(x)
返回的数字是705032703。
我将golang代码转换为C代码如下:
main.go
package main
import "C"
func helloWorld(x int64) int64 {
s := int64(1)
for i := int64(1); i < x; i++ {
s = i
}
return s
}
英文:
I converted the golang code to c code and called it from python. but when the function should return a number close to the number I wrote inside, it returns a very different number.
main.py
import ctypes
library = ctypes.cdll.LoadLibrary('./maintain.so')
hello_world = library.helloWorld
numb = 5000000000
n = ctypes.c_int64(numb)
x = hello_world(n)
print(x)
returning number: 705032703
golang code that I converted to c code
main.go
package main
import "C"
func helloWorld(x int64) int64 {
s := int64(1)
for i := int64(1); i < x; i++ {
s = i
}
return s
}
答案1
得分: 3
你犯了99%新的ctypes
用户都会犯的错误:没有声明所使用函数的参数类型和返回类型。ctypes
默认将标量参数视为c_int
,指针参数视为c_void_p
,返回类型默认为c_int
,除非另有说明。如果你定义了它们,你就不需要将每个参数都包装在你想要传递的类型中,因为ctypes
已经知道了。
我无法为Go设置,但这是一个带有64位参数和返回类型的简单C实现的函数:
#include <stdint.h>
#ifdef _WIN32
# define API __declspec(dllexport)
#else
# define API
#endif
API int64_t helloWorld(int64_t x) {
return x + 1;
}
调用它的Python代码:
import ctypes as ct
dll = ct.CDLL('./test')
dll.helloWorld.argtypes = ct.c_int64, # 参数类型的序列
dll.helloWorld.restype = ct.c_int64 # 返回类型
# 注意,你不需要将参数包装起来,比如c_int64(5000000000)。
print(dll.helloWorld(5_000_000_000))
输出:
5000000001
英文:
You're making the mistake 99% of new ctypes
users: not declaring the argument types and return type of the function used. ctypes
assumes c_int
for scalars and c_void_p
for pointers on arguments and c_int
for return type unless told otherwise. If you define them, you don't have to wrap every parameter in the type you want to pass, because ctypes
will already know.
I'm not set up for Go, but here's a simple C implementation of the function with a 64-bit argument and return type:
#include <stdint.h>
#ifdef _WIN32
# define API __declspec(dllexport)
#else
# define API
#endif
API int64_t helloWorld(int64_t x) {
return x + 1;
}
The Python code to call it:
import ctypes as ct
dll = ct.CDLL('./test')
dll.helloWorld.argtypes = ct.c_int64, # sequence of argument types
dll.helloWorld.restype = ct.c_int64 # return type
# Note you don't have to wrap the argument, e.g. c_int64(5000000000).
print(dll.helloWorld(5_000_000_000))
Output:
5000000001
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论