Skip to content

Commit 4bf7764

Browse files
committed
fix issue in malloc
1 parent a5f1780 commit 4bf7764

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

examples/module1/struct/main.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
package main
22

33
import (
4+
"encoding/json"
5+
"fmt"
46
"reflect"
57
)
68

79
type MyType struct {
810
Name string `json:"name"`
11+
Address
12+
}
13+
type Address struct {
14+
City string `json:"city"`
915
}
10-
1116
func main() {
12-
mt := MyType{Name: "test"}
17+
mt := MyType{Name: "test",Address: Address{City: "shanghai"}}
18+
b, _ := json.Marshal(&mt)
19+
fmt.Println(string(b))
1320
myType := reflect.TypeOf(mt)
1421
name := myType.Field(0)
1522
tag := name.Tag.Get("json")

examples/module3/malloc/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ import (
1010

1111
func main() {
1212
// only loop 10 times to avoid exhausting the host memory
13+
holder := []*C.char{}
1314
for i := 1; i <= 10; i++ {
14-
// allocMemory malloc and memset 100Mb per time
1515
fmt.Printf("Allocating %dMb memory, raw memory is %d\n", i*100, i*100*1024*1025)
16-
C.allocMemory()
16+
// hold the memory, otherwise it will be freed by GC
17+
holder = append(holder, (*C.char)(C.allocMemory()))
1718
time.Sleep(time.Minute)
1819
}
1920
}

0 commit comments

Comments
 (0)