Hi Ian, Thank you for the response.Got it. Basically, fmt.Println(InitStruct) is printing the values of fields, cb and data, for the struct Foo pointed by InitStruct. fmt.Printf("%p\n", InitStruct) is printing the memory address. Btw..can you please clarify the following :
1)Is this the correct way to pass a C struct from a Go code to C code. 2) Can I free the struct memory assigned to Initstruct using calloc on Go side in the following manner ? if InitStruct != nil{ C.free(unsafe.Pointer(InitStruct)) } Thanks, Nitish On Wed, Mar 4, 2020 at 11:19 PM Ian Lance Taylor <i...@golang.org> wrote: > On Wed, Mar 4, 2020 at 3:57 AM Nitish Saboo <nitish.sabo...@gmail.com> > wrote: > > > > I have CGO project. > > > > Following is my C header file and Go code > > > > syslog-node.h > > =============== > > > > #ifndef _TEST_H_ > > #define _TEST_H_ > > > > #include <stdlib.h> > > > > typedef void (*key_value_cb)(const char* key, const char* value, size_t > value_len, int work); > > typedef struct Foo{ > > key_value_cb cb; > > int data; > > }Foo; > > int initialize_engine(const char* filename, const char* module_path, Foo > *cb); > > int reload_pattern_db(const char* filename, Foo *cb); > > > > > > cfunc.go > > ======== > > /* > > > > #include <stdio.h> > > > > // The gateway function > > void callOnMeGo_cgo(char *key, char *value, size_t value_len, int work) > > { > > void ParsedData(const char *key, const char *value, size_t value_len, > int work); > > ParsedData(key, value, value_len, work); > > } > > */ > > import "C" > > > > main.go > > ======== > > > > var InitStruct *C.struct_Foo; > > > > func InitializeEngine(pattern string, path string) { > > pattern_db := C.CString(pattern) > > module_path := C.CString(path) > > if InitStruct != nil{ > > C.free(unsafe.Pointer(InitStruct)) > > } > > InitStruct := (*C.Foo)(C.calloc(1, C.sizeof_struct_Foo)) > > InitStruct.cb = (C.key_value_cb)(C.callOnMeGo_cgo) > > InitStruct.data = C.int(0) > > fmt.Println(InitStruct) <<<<<<<<<<<<<<<<<<<<<<<<<<<'&{0x4b79d0 0 [0 0 0 > 0]}' got printed > > C.initialize_engine(pattern_db, module_path, InitStruct) > > > > } > > > > func ReloadPatternDB(patterndb string) { > > > > path := C.CString(patterndb) > > InitStruct = (*C.Foo)(C.calloc(1, C.sizeof_struct_Foo)) > <<<<<<<Allocating a new memory > > InitStruct.cb = (C.key_value_cb)(C.callOnMeGo_cgo) > > InitStruct.data = C.int(0) > > fmt.Println(InitStruct) <<<<<<<<<<<<<<<<<<<<<<<<<<< '&{0x4b79d0 0 [0 > 0 0 0]}' got printed > > C.reload_pattern_db(path, InitStruct) > > > > } > > > > > > I have a Go code where I have to call C functions 'initialize_engine' > and 'reload_pattern_db' respectively(one after the other ) and pass the C > struct from Go code. > > > > > > The issue is after allocating memory to 'InitStruct' using calloc in > 'InitializeEngine' method, the same memory location is getting printed in > ReloadPatternDB method even though I am allocating a new memory to > 'InitStruct' using calloc in 'ReloadPatternDB' method. > > > > 1) Is this the correct way to pass a C struct from a Go code to C code. > > 2) Is this a correct way to free the struct memory assigned to > InitStruct using calloc on Go side ? > > You haven't showed us where the same memory location is getting > printed. Note that fmt.Println is not printing the address of > InitStruct; it is printing the values of the fields. If you want to > print the address, use fmt.Printf("%p\n", InitStruct). > > Also note that InitStruct is a global variable. > > Ian > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CALjMrq5d-vh-StGaXJe3-aAAsajcrK%3D%3DDxe%3DOx_vxgW%3D1vvRSQ%40mail.gmail.com.