-
Notifications
You must be signed in to change notification settings - Fork 18k
encoding/gob: oom on doubly linked list #66951
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Labels
Comments
Below is the demo code. It cannot work. package main
import (
"encoding/gob"
"fmt"
"log"
"os"
)
type Node struct {
Val int
Prev *Node
Next *Node
}
func main() {
// Creating doubly linked list
head := &Node{Val: 1}
head.Next = &Node{Val: 2, Prev: head}
head.Next.Next = &Node{Val: 3, Prev: head.Next}
// Create and open file to store data
file, err := os.Create("store.gob")
if err != nil {
log.Fatal("create file error:", err)
}
defer file.Close()
// Create encoder and encode structure to file
enc := gob.NewEncoder(file)
if err = enc.Encode(head); err != nil {
log.Fatal("encode error:", err)
}
// Close the file
file.Close()
// Open file to be ready for decoding
file, err = os.Open("store.gob")
if err != nil {
log.Fatal("open file error:", err)
}
defer file.Close()
// Create decoder and decode structure from file
var decodedHead Node
dec := gob.NewDecoder(file)
if err = dec.Decode(&decodedHead); err != nil {
log.Fatal("decode error:", err)
}
// Printing decoded data
for node := &decodedHead; node != nil; node = node.Next {
fmt.Println(node.Val)
}
} |
The singly linked list works perfect. package main
import (
"encoding/gob"
"fmt"
"log"
"os"
)
type Node struct {
Val int
Next *Node
}
func main() {
// Creating singly linked list
head := &Node{Val: 1}
head.Next = &Node{Val: 2}
head.Next.Next = &Node{Val: 3}
// Create and open file to store data
file, err := os.Create("store.gob")
if err != nil {
log.Fatal("creation file error:", err)
}
defer file.Close()
// Create encoder and encode structure to file
enc := gob.NewEncoder(file)
if err = enc.Encode(head); err != nil {
log.Fatal("encoding error:", err)
}
// Close the file
file.Close()
// Open file for decoding
file, err = os.Open("store.gob")
if err != nil {
log.Fatal("opening file error:", err)
}
defer file.Close()
// Create decoder and decode structure from file
var decodedHead Node
dec := gob.NewDecoder(file)
if err = dec.Decode(&decodedHead); err != nil {
log.Fatal("decoding error:", err)
}
// Printing the decoded list
for node := &decodedHead; node != nil; node = node.Next {
fmt.Println(node.Val)
}
} |
from the package docs
|
thanks for your reply. |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Go version
go version go1.22.2 linux/amd64
Output of
go env
in your module/workspace:What did you do?
I use gob.Encode to save a doubly linked list.
This is the struct
What did you see happen?
it will casue oom.
What did you expect to see?
save to file success
The text was updated successfully, but these errors were encountered: