I'm playing a bit with GDB + golang and I've found something that I'm not
really sure I understand why it is like that or if it's ok to be like that
or not.
If this has been answered before, I apologize, I've searched the group but
I haven't found something like this, please point me to the right post.
See below the program, output, environment and my questions:
demo.go: http://play.golang.org/p/dGBT7xECRs
package main
import "fmt"
func main() {
a := "Hello world!"
b := 1.12
c := false
var d [3]int
e := 2.21
f := b + e
if c {
fmt.Printf("a: %s \n", a)
fmt.Printf("f: %f \n", f)
fmt.Printf("f: %v \n", f)
} else {
fmt.Printf("f: %v \n", f)
fmt.Printf("f: %f \n", f)
fmt.Printf("a: %s \n", a)
}
fmt.Println("d: ", d)
}
------------------------------
Build command: go build -gcflags "-N -l" -o demo demo.go
gdb demo
------------------------------
break demo.go:6
break demo.go:13
run
info locals
a = <error reading variable: Address 0x4411e6 out of bounds>break demo.go:13
run
info locals
b = 6,9533491646583695e-310
c = false
d = {140737353895936, 0, 100, 4316562, 16, 4816288, 0, 4461078, 4816288,
833492132160, 0, 100, 833492132160, 4409578, 4979216, 29, 140737353932416,
833492132160, 65, 4286192, 4198466, 4272079, 5651336, 0, 0, 4271792,
4294967296, -1, 0, 5651296, 0, 0, 4281584, 0 <repeats 96 times>}
e = 4,9406564584124654e-322
f = 0
run
info locals
info localinfo locals
a = "Hello world!"
b = 1,1200000000000001
c = false
d = {0, 0, 0, 4316562, 16, 4816288, 0, 4921840, 12, 833492132160, 0, 100,
833492132160, 4409578, 4979216, 29, 140737353932416, 833492132160, 65,
4286192, 4198466, 4272079, 5651336, 0, 0, 4271792, 4294967296, -1, 0,
5651296, 0, 0, 4281584, 0 <repeats 96 times>}
e = 2,21
f = 3,3300000000000001
------------------------------
I'm using GO 1.2 from the official distribution on Ubuntu 13.10 with
kernel 3.12.7, GNU gdb (GDB) 7.6.1-ubuntu.
Since I'm running on this version of OS I've had to run 2to3 to convert the
runtime-gdb.py script to python3.
Here's what I don't understand:
- why the variables look like that in memory before they are assigned in
that form? Would it be better to have them initialized to 0? Is it because
of the kernel / way memory is allocated by the go runtime or I'm way of?
- why does the 'd' variable looks like that? While I could understand it
before initialization, even if it should be 0 sized but maybe the answer is
related to the above question, after the initialization shouldn't it be
limited to only three values? I've tried with something like "var d [6]int"
but then the number of elements goes down and it looks like this: "d =
{4715168, 100, 140737353895984, 140737353895936, 0, 100, 4316562, 16,
4816160, 0, 4461078, 4816160, 833492132160, 0, 100, 833492132160, 4409578,
4979056, 29, 140737353932416, 833492132160, 65, 4286192, 4198466, 4272079,
5651336, 0, 0, 4271792, 4294967296, -1, 0, 5651296, 0, 0, 4281584, 0
<repeats 61 times>}"
- why does "%v" produces a different output that "%f". According to the
documentation: "%v the value in a default format." so shouldn't the output
in this case be the one that "%f" produces?
I know that running the program will produce the expected output, besides
the last question, but I'm just curious why this is like it is.
Also, I would hope that the second point about the int array is just a
quirk in output from gdb, doesn't happen in normal programs (but I have no
clue how to check it out).
Note: I've tried GO 1.1.2 and it produces the same things.
Thank you for your time.
Kind regards,
Florin
--
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.