duandi8613 2016-01-26 03:46 采纳率: 100%
浏览 42
已采纳

为什么地址大小不同? (0x206a10-0x104382e0)

http://play.golang.org/p/BgnHN-GikU

var p1 = new(int)
var p2 *int = new(int)
var p3 = 0
var p4 *int

func main() {
    fmt.Println(*p1, &p1, p1)
    fmt.Println()
    fmt.Println(*p2, &p2, p2)
    fmt.Println()
    fmt.Println(p3, &p3)
    fmt.Println()
    fmt.Println(p4, &p4)
}

0 0x206a10 0x104382e0

0 0x206a14 0x104382f0

0 0x21ccc0

<nil> 0x206a18

  • 写回答

1条回答 默认 最新

  • douyin8623 2016-01-26 03:58
    关注

    It's not the address size what is different, it's the size (length) of the hexadecimal representation as you printed it.

    All addresses you print on the Go Playground are 4 bytes, but if the first bytes (or bits) are zero, they are not printed.

    Also if you take a closer look, addresses of your global vars have 6 hexa digits, pointers allocated and returned by new() have 8 hexa digits. This is because those ints (returned by new()) are allocated on the heap with bigger offset (and thus "bigger" memory address).

    For example:

    var i, j int32 = 123, 123000
    fmt.Printf("%x %x
    ", i, j)
    

    Prints 7b 1e078 even though both numbers are 4 bytes long (32 bits). You may use a format string to add padding 0s like this:

    fmt.Printf("%08x %08x
    ", i, j)
    

    Which results in 0000007b 0001e078 but this will always pad to 8 digits even if i or j would be "less" than 4 bytes (e.g. int16), so this padding won't tell you if they are of different sizes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题