我试图通过将其参数保存在void指针列表中来推迟函数调用(使用函数包装器):
void *args[]
int argt[]
Run Code Online (Sandbox Code Playgroud)
argt用于记住存储在void*位置的数据类型.
后来,我需要调用推迟的函数:
function(args[0], args[1])
Run Code Online (Sandbox Code Playgroud)
但问题是我必须正确指定他们的类型.
我使用宏,像这样:
#define ARGTYPE(arg, type) type == CHARP ? (char *) arg : (type == LONGLONG ? *((long long *) arg) : NULL)
Run Code Online (Sandbox Code Playgroud)
并且函数调用变为:
function(ARGTYPE(args[0], argt[0]), ARGTYPE(args[1], argt[1]))
Run Code Online (Sandbox Code Playgroud)
我有两个问题:
1)警告:条件表达式中的指针/整数类型不匹配,由宏定义生成(请注意我可以忍受它,见2))
2)真正的问题:long long参数没有正确传递(我每次都得0)
我显然遗漏了一些东西,所以有人可以解释(详细)为什么宏不能正常工作,或者提出另一种方法吗?
EDIT:我在这里添加存储参数部分(相关细节,我解析一个va_list),它根据格式说明符得到它们的类型:
while (*format)
{
switch(*format)
{
case 's':
saved_arguments[i] = strdup(arg);
break;
case 'l':
saved_arguments[i] = malloc(sizeof(long long));
*((long long *) saved_arguments[i]) = arg;
break;
}
i++;
format++;
}
Run Code Online (Sandbox Code Playgroud) 我正在为启动服务脚本编写一个停止例程:
do_stop()
{
rm -f $PIDFILE
pkill -f $DAEMON || return 1
return 0
}
Run Code Online (Sandbox Code Playgroud)
问题是pkill(与killall相同)也匹配代表脚本本身的进程,它基本上终止了自己.如何解决?
所以,我定义了一个简单的生成器:
def gen1(x):
if x <= 10:
yield x
for v in gen1(x + 1):
yield v
Run Code Online (Sandbox Code Playgroud)
基本上,我想装饰它,所以它返回所有值,但最后:
def dec(gen):
def new_gen(x):
g = gen(x)
value = g.next()
for v in g:
yield value
value = v
return new_gen
Run Code Online (Sandbox Code Playgroud)
现在,如果我重新定义gen1
@dec
def gen1(x):
...
for i in gen1(1):
print i # Nothing printed
Run Code Online (Sandbox Code Playgroud)
但如果我使用:
some_gen = dec(gen1)
for i in some_gen(1):
print i # Prints 1 to 9, as needed
Run Code Online (Sandbox Code Playgroud)
为什么我的装饰师不起作用,我该如何解决?
在无符号整数中交换第一个(最低有效)2个不同连续位的快速而优雅的方法是什么?
例如
100100 -> 100010
110011 -> 110101
Run Code Online (Sandbox Code Playgroud)
到目前为止,我想出了这个:
unsigned long long special_swap(unsigned long long number)
{
if (number & 1)
return (number + 1) ^ ((number ^ (number + 1)) >> 2);
number = ~number;
return ~((number + 1) ^ ((number ^ (number + 1)) >> 2));
}
Run Code Online (Sandbox Code Playgroud)
我对上述解决方案的最大不满是它使用了该if指令.
我开始学习Go并且无法理解以下内容:
package main
import "fmt"
type I interface {
foo(interface {})
}
type S struct {}
func (s S) foo(i int) {
fmt.Println(i)
}
func main() {
var i I = S{}
i.foo(2)
}
Run Code Online (Sandbox Code Playgroud)
这失败了:
cannot use S literal (type S) as type I in assignment:
S does not implement I (wrong type for foo method)
have foo(int)
want foo(interface {})
Run Code Online (Sandbox Code Playgroud)
我不明白为什么Go不接受foo(int)签名,因为int实现的事实interface {}.任何人都可以帮忙解释一下吗?
我写了一个小c程序:
#include <stdio.h>
int main()
{
char s[] = "Hello, world!";
printf("%s\n", s);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译到(在我的linux机器上):
.file "hello.c"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
movq %rsp, %rbp
.cfi_def_cfa_register 6
subq $32, %rsp
movq %fs:40, %rax
movq %rax, -8(%rbp)
xorl %eax, %eax
movl $1819043144, -32(%rbp)
movl $1998597231, -28(%rbp)
movl $1684828783, -24(%rbp)
movw $33, -20(%rbp)
leaq -32(%rbp), %rax
movq %rax, %rdi
call puts
movl $0, %eax
movq -8(%rbp), %rdx
xorq %fs:40, …Run Code Online (Sandbox Code Playgroud) 假设我有一个 C 库,代码如下:
typedef int (callback_t)(int);
void register_callback(callback_t cb);
Run Code Online (Sandbox Code Playgroud)
我想为此函数编写 go 绑定并传递任意 go 回调。
我在这里找到了一个很好的答案。然而,这是一个利用回调接受 a 的事实的技巧void *,Go 通过它将函数指针传递给 C 并接收它。但是,这不能应用于我的示例,因为没有 user void *。
我能做的最好的事情是:
/*
extern int gobridge(int data);
static int cbridge(void)
{
register_callback(gobridge);
}
*/
import "C"
type Callback func (int) int
var g_cb Callback
//export gobridge
func gobridge(data C.int) C.int {
return C.int(g_cb(int(data)))
}
func RegisterCallback(cb Callback) {
g_cb = cb //save callback in a global
C.cbridge()
}
func Count(left, right int) { …Run Code Online (Sandbox Code Playgroud)