Thankyou Ian for your responses.

Thanks,
Nitish


On Fri, 6 Mar 2020, 21:01 Ian Lance Taylor, <i...@golang.org> wrote:

> On Fri, Mar 6, 2020 at 7:18 AM Nitish Saboo <nitish.sabo...@gmail.com>
> wrote:
> >
> > > 3) Why is 'fmt.Println(InitStruct)' printing the struct fields even
> after freeing the memory ? [b]
> >
> > Because that's how C works.  C is not a memory safe language, and it
> > doesn't become memory safe just because you make the calls from Go.
> >
> > 1)You mean to say the dangling pointer will return the struct fields
> unless we make it 'nil' explicitly ..am i correct ?
>
> What you get by dereferencing a dangling pointer is unpredictable.  If
> you dereference it immediately after the C.free, as you are doing, you
> will typically get the same values as before the C.free.  Even that
> can vary depending on the behavior of the C memory allocator.
>
> If you set the pointer to nil, then you no longer have a dangling pointer.
>
> https://en.wikipedia.org/wiki/Dangling_pointer
>
> > 2) Here, C.free() will completely free the memory allocation ...right ?
>
> Yes, it will completely free the memory allocated by C.calloc.
>
> Ian
>
>
> > On Fri, Mar 6, 2020 at 7:55 PM Ian Lance Taylor <i...@golang.org> wrote:
> >>
> >> On Fri, Mar 6, 2020 at 4:25 AM Nitish Saboo <nitish.sabo...@gmail.com>
> wrote:
> >> >
> >> > So what did I do :
> >> >
> >> > 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(5)
> >> > fmt.Println(InitStruct)  <<<<<<<<<<<<<<<<<<<<'&{0x4b7cb0 5 [0 0 0
> 0]}' got printed
> >> > fmt.Printf("%p\n", InitStruct)
> <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<'0x1905630' got printed
> >> > C.initialize_engine(pattern_db, module_path, InitStruct)
> >> >
> >> > }
> >> >
> >> > func ReloadPatternDB(patterndb string) {
> >> >
> >> > if InitStruct != nil{
> >> > fmt.Println(InitStruct) <<<<<<<<<<<<<<<'&{0x4b7cb0 5 [0 0 0 0]}' got
> printed
> >> > fmt.Printf("%p\n", InitStruct) <<<<<<<<<<<<<'0x1905630' got printed
> >> > C.free(unsafe.Pointer(InitStruct))
> >> > fmt.Printf("%p\n", InitStruct). <<<<<<<<<<<'0x1905630' got printed
> ...Older memory address getting printed even after freeing the memory [a]
> >> > fmt.Println(InitStruct). <<<<<<<<<<<<<<'&{0x863b470 5 [0 0 0 0]}' got
> printed. Why is struct fields getting printed? [b]
> >> > }
> >> > path := C.CString(patterndb)
> >> > InitStruct = (*C.Foo)(C.calloc(1, C.sizeof_struct_Foo))
> >> > InitStruct.cb = (C.key_value_cb)(C.callOnMeGo_cgo)
> >> > InitStruct.data = C.int(5)
> >> > fmt.Println(InitStruct)    <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
> '&{0x4b7cb0 5 [0 0 0 0]}' got printed
> >> > C.reload_pattern_db(path, InitStruct) <<<<<<<<<. '0x9031d40' got
> printed
> >> >
> >> > }
> >> >
> >> >
> >> > InitStruct is a global variable.I am calling C functions
> 'initialize_engine' and 'reload_pattern_db' respectively.
> >> >
> >> > 1) Even after freeing the memory in 'reload_pattern_db', the older
> memory address is getting printed.Why? [a]
> >>
> >> Here you are using calls to C.calloc and C.free, so you are using C
> >> pointers and C memory allocation.  That is fine.  But in C, calling
> >> C.free doesn't somehow zero out the pointer.  It leaves the pointer
> >> unchanged, and it becomes a dangling pointer that is unsafe to use.
> >> Basically, if you use C calls, you get C behavior.  That is true even
> >> when calling the C functions from Go.
> >>
> >> > 2) Do I have to explicitly point 'InitStruct=nil' after freeing the
> memory.
> >>
> >> Yes, that will remove the dangling pointer.
> >>
> >> > 3) Why is 'fmt.Println(InitStruct)' printing the struct fields even
> after freeing the memory ? [b]
> >>
> >> Because that's how C works.  C is not a memory safe language, and it
> >> doesn't become memory safe just because you make the calls from Go.
> >>
> >> 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/CALjMrq7WUzRhU%3Dj2O-SxpYGNhPBm2-8oSfVcOtmVUDP8%2BEw3BQ%40mail.gmail.com.

Reply via email to