小编Wya*_*att的帖子

在golang中初始化C结构时,struct initializer中的值太少

我已经尝试了以下程序,但它在编译时告诉我"struct initializer中的值太少".

package main

/*
#include <stdlib.h>
struct Person {
    char *name;
    int age;
    int height;
    int weight;
};
*/
import "C"
import "fmt"

type p C.struct_Person

func main() {

    person := p{C.CString("Giorgis"), 30, 6, 175}
    fmt.Println(person)
    fmt.Println(C.GoString(person.name))
    fmt.Println(person.age)
    fmt.Println(person.height)
    fmt.Println(person.weight)
}
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个有线问题?另外,当我将类型"char*"更改为"char"和初始化程序时.它运作良好.

struct Person {
    char name;
    int age;
    int height;
    int weight;
};
Run Code Online (Sandbox Code Playgroud)

另外,当我使用时

struct Person {
    char *name;
};
Run Code Online (Sandbox Code Playgroud)

它也运作良好.

无论如何,我该如何解决?谢谢.

go cgo

5
推荐指数
1
解决办法
3605
查看次数

golang中的内存泄漏

这是代码.在golang主函数中,在main.go中

func main() {
    rgc.GetRgcService()
}
Run Code Online (Sandbox Code Playgroud)

其中,rgc在另一golang文件,命名mrgc.go.里面的代码是

package rgc
func GetRgcService() (svc *RgcService, err error) {}
Run Code Online (Sandbox Code Playgroud)

函数GetRgcService是一个空函数.

但是,当我valgrind以前测试内存时,我得到了以下输出

 ==58156== HEAP SUMMARY:
 ==58156==     in use at exit: 1,152 bytes in 4 blocks
 ==58156==   total heap usage: 9 allocs, 5 frees, 1,304 bytes allocated
 ==58156== 288 bytes in 1 blocks are possibly lost in loss record 4 of 4
 ==58156==    at 0x4A27F63: calloc (vg_replace_malloc.c:593)
 ==58156==    by 0x4010DE1: allocate_dtv (in /home/opt/gcc-4.8.2.bpkg-r2/gcc-4.8.2.bpkg-r2/lib64/ld-2.18.so)
==58156==    by 0x40114ED: _dl_allocate_tls (in /home/opt/gcc-4.8.2.bpkg-r2/gcc-4.8.2.bpkg-r2/lib64/ld-2.18.so) …
Run Code Online (Sandbox Code Playgroud)

memory-leaks go

2
推荐指数
1
解决办法
5169
查看次数

标签 统计

go ×2

cgo ×1

memory-leaks ×1