{"id":207560,"date":"2025-07-08T13:42:48","date_gmt":"2025-07-08T05:42:48","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207560\/"},"modified":"2025-07-08T13:42:48","modified_gmt":"2025-07-08T05:42:48","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-go-%e6%8c%87%e9%92%88%e4%b8%8e-uintptr-%e4%b9%8b%e9%97%b4%e8%bf%9b%e8%a1%8c%e8%bd%ac%e6%8d%a2%e8%80%8c%e4%b8%8d%e9%80%a0%e6%88%90%e5%86%85%e5%ad%98%e6%8d%9f%e5%9d%8f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207560\/","title":{"rendered":"\u5982\u4f55\u5728 Go \u6307\u9488\u4e0e uintptr \u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u800c\u4e0d\u9020\u6210\u5185\u5b58\u635f\u574f\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5982\u4f55\u5728 Go \u6307\u9488\u4e0e uintptr \u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u800c\u4e0d\u9020\u6210\u5185\u5b58\u635f\u574f\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-23 17:00:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5728 Go \u6307\u9488\u4e0e uintptr \u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u800c\u4e0d\u9020\u6210\u5185\u5b58\u635f\u574f\uff1f\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6307\u9488\u4e4b\u95f4\u7684\u8f6c\u6362\u5e94\u4f7f\u7528 <code>unsafe.pointer()<\/code> \u548c <code>uintptr<\/code> \u8fdb\u884c\u3002 \u6211\u6b63\u5728\u4f7f\u7528 go \u7f16\u5199\u4e00\u4e2a\u89e3\u91ca\u5668\u3002\u8fd9\u662f\u975e\u5e38\u7b80\u5355\u7684\u7247\u6bb5\uff0c\u4f7f\u7528 <code>eid<\/code> \u7ed3\u6784\u5728\u672c\u673a\u4ee3\u7801\u7684\u4e0d\u540c\u90e8\u5206\u4e4b\u95f4\u643a\u5e26\u5bf9\uff08\u7c7b\u578b\uff0c\u503c\uff09\u3002\u8fd9\u6bb5\u4ee3\u7801\u4ee4\u4eba\u60ca\u8bb6\uff0c\u56e0\u4e3a\u76f8\u540c\u7684 print \u8bed\u53e5\u83b7\u5f97\u4e24\u4e2a\u4e0d\u540c\u7684\u503c\uff08\u8bf7\u53c2\u9605 <code>foo()<\/code> \u65b9\u6cd5\uff09\u3002\u8be5\u5bf9\u8c61\u88ab\u201c\u5c01\u88c5\u201d\u5230 <code>eid<\/code> \u4e2d\u5e76\u8f6c\u6362\u56de\u5bf9\u8c61\u3002<\/p>\n<p> \u4ee3\u7801\u53ef\u4ee5\u7f16\u8bd1\uff0c\u4f46\u7ed3\u679c\u4e25\u91cd\u635f\u574f\u3002<\/p>\n<p>\u5982\u679c\u4f60\u8fd0\u884c\u8fd9\u4e2a\u4f60\u4f1a\u5f97\u5230\uff1a<\/p>\n<pre>~\/go% &nbsp;go run testbug.go\ncreate object 0xc000068e28 with class 0xc00000c060\nhere is y:0xc000068e28, y.isa: 0xc00000c060\"\nhere is y:0xc000068e28, y.isa: 0x2c<\/pre>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"unsafe\"\n)\n\ntype EID struct {\n    SORT *Class\n    VAL  uintptr\n}\n\n\/\/ access to VAL\nfunc OBJ(x EID) *Anything { return (*Anything)(unsafe.Pointer(x.VAL)) }\nfunc INT(x EID) int       { return (int)((uintptr)(unsafe.Pointer(x.VAL))) }\n\n\/\/ useful utility get the pointer as a uintptr\nfunc (x *Anything) Uip() uintptr { return uintptr(unsafe.Pointer(x)) }\n\ntype Anything struct {\n    Isa *Class\n}\n\nfunc (x *Anything) Id() *Anything { return x }\n\ntype Object struct {\n    Anything\n    name string\n}\n\ntype Class struct {\n    Object\n    Super *Class\n}\n\ntype Integer struct {\n    Anything\n    Value int\n}\n\nfunc MakeObject(c *Class) *Anything {\n    o := new(Object)\n    o.Isa = c\n    return o.Id()\n}\n\n\/\/ this is the surprising example - EID is passed but the content is damaged\nfunc (c *Class) Foo() EID {\n    x := c.Bar()\n    y := OBJ(x)\n    z := y.Isa\n    fmt.Printf(\"here is y:%p, y.Isa: %p\\n\", y, z)\n    fmt.Printf(\"here is y:%p, y.Isa: %p\\n\", y, y.Isa)  \/\/ this produces a different value !\n    return x\n}\n\nfunc (c *Class) Bar() EID {\n    UU := EID{c, MakeObject(c).Uip()}\n    fmt.Printf(\"create object %p with class %p\\n\", OBJ(UU), OBJ(UU).Isa)\n    return UU\n}\n\nvar aClass *Class\nvar aInteger *Class\n\nfunc main() {\n    aClass := new(Class)\n    aClass.Isa = aClass\n    aClass.Foo()\n}<\/pre>\n<p>\u663e\u7136\uff0c\u6307\u5411\u6307\u9488\u7684 uintptr \u5fc5\u987b\u662f\u672c\u5730\u7684\uff0c\u5e76\u4e14\u4e0d\u80fd\u53d1\u751f\u5728\u4e24\u4e2a\u4e0d\u540c\u7684\u5730\u65b9\uff08\u6b64\u5904\u4e3a foo() \u548c bar()\uff09\u3002\u6211\u627e\u5230\u4e86\u89e3\u51b3\u65b9\u6cd5\uff0c\u4f46\u6211\u5bf9\u8fd9\u79cd\u5947\u602a\u7684\u884c\u4e3a\u611f\u5230\u597d\u5947\u3002<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u5f53\u60a8\u5c06\u6307\u9488\uff08\u4efb\u4f55\u5177\u4f53\u7c7b\u578b\uff0c\u751a\u81f3 <code>unsafe.pointer<\/code> \u7c7b\u578b\uff09\u5b58\u50a8\u5230 <code>uintptr<\/code> \u4e2d\u65f6\uff0c\u8fd9\u4f1a\u5411 go \u7684\u5783\u573e\u6536\u96c6\u5668\u9690\u85cf\u6307\u9488\u3002\u56e0\u6b64\uff0c\u5982\u679c\u6ca1\u6709<em>\u5176\u4ed6<\/em>\u6307\u5411\u5b83\u7684\u6307\u9488\uff0cgo\u53ef\u4ee5\u81ea\u7531\u5730gc\u5e95\u5c42\u5bf9\u8c61\u3002<\/p>\n<p>\u5f53\u60a8\u5c06 <code>uintptr<\/code> \u8f6c\u6362\u4e3a <code>unsafe.pointer<\/code> \u65f6\uff0c\u8be5\u5bf9\u8c61\uff08\u5b58\u50a8\u5728 <code>uintptr<\/code> \u4e2d\u7684\u503c\u6240\u8f6c\u6362\u7684\u6307\u9488\uff09\u9700\u8981<em>\u5b58\u5728<\/em>\u3002\u5982\u679c\u5b83\u88ab gc \u4e86\uff0c\u5b83\u5c31\u4e0d\u518d\u5b58\u5728\u3002\u56e0\u6b64\uff0c\u83b7\u53d6\u4efb\u4f55\u7c7b\u578b <code>*t<\/code> \u7684\u67d0\u4e9b\u6307\u9488\u503c\u5e76\u5c06\u5176\u5b58\u50a8\u5728 <code>uintptr<\/code> \u4e2d\u7684\u201c\u5b89\u5168\u201d\u65b9\u6cd5\u662f\u5c06\u5176\u5b58\u50a8\u5728 <code>unsafe.pointer<\/code> \u4e2d\u3002 <code>unsafe.pointer<\/code> \u5bf9\u8c61\u5bf9\u4e8e go \u7684\u5783\u573e\u6536\u96c6\u5668\u6765\u8bf4\u662f\u53ef\u89c1\u7684\uff0c\u4f5c\u4e3a\u4e00\u4e2a\u6307\u9488\uff0c\u56e0\u6b64\u8fd9\u4f7f\u5b9e\u9645\u5bf9\u8c61\u4fdd\u6301\u6d3b\u52a8\u72b6\u6001\u3002<\/p>\n<p>\u60a8\u5c06\u5728\u4e00\u4e9b go \u5185\u90e8\u8f6f\u4ef6\u4e2d\u770b\u5230\u8fd9\u79cd\u6a21\u5f0f\uff1a<\/p>\n<pre>\/\/ need to keep the pointer alive while we make a syscall\np := unsafe.Pointer(foo)\nret := syscall.SyscallN(..., uintptr(foo), ...)<\/pre>\n<p>\u5c40\u90e8\u53d8\u91cf <code>p<\/code> \u7684\u521b\u5efa\u663e\u7136\u6beb\u65e0\u610f\u4e49\uff0c\u7528\u4e8e\u4fdd\u62a4\u5e95\u5c42\u5bf9\u8c61\u5728\u64cd\u4f5c\u7cfb\u7edf\u7cfb\u7edf\u8c03\u7528\u8bfb\u53d6\u5176\u5b57\u8282\u65f6\u4e0d\u88ab gc \u56de\u6536\u3002 \uff08\u8bf7\u6ce8\u610f\uff0c\u8fd9\u4e0e\u7f16\u8bd1\u5668\u8fc7\u4e8e\u53cb\u597d\uff0c\u56e0\u4e3a\u5bf9 <code>p<\/code> \u7684\u8d4b\u503c\u5728\u8fd9\u91cc\u4f3c\u4e4e\u662f\u6b7b\u4ee3\u7801\u3002\u4e5f\u8bb8\u5185\u90e8\u8f6f\u4ef6\u6bd4\u8fd9\u66f4\u5947\u7279\uff0c\u548c\/\u6216\u4ed6\u4eec\u4f7f\u7528 <code>\/\/go:...<\/code> \u6ce8\u91ca\u4e3a\u597d\u5427\u3002\uff09<\/p>\n<p>\u5982\u679c\u6211\u91c7\u7528\u4f60\u7684\u975e\u6700\u5c0f\u53ef\u91cd\u590d\u793a\u4f8b\u5e76\u5bf9\u5b83\u8fdb\u884c\u660e\u663e\u7684\u6700\u5c0f\u66f4\u6539\uff0c\u90a3\u4e48\u540c\u6837\u7684\u6a21\u5f0f\u5728 go \u6f14\u793a\u4e2d\u4e5f\u9002\u7528\u3002\u8fd9\u662f\u5426\u8db3\u591f\uff08\u4ee5\u53ca\u60a8\u5e0c\u671b\u5982\u4f55\u5728\u89e3\u91ca\u5668\u4e2d\u4f7f\u7528\u76f8\u540c\u7684\u6982\u5ff5\uff09\u5b8c\u5168\u662f\u53e6\u4e00\u4e2a\u95ee\u9898\uff0c\u4f46\u8bf7\u53c2\u9605 \u3002 \u6ce8\u610f\uff1a\u6211\u5fc5\u987b\u5411\u60a8\u7684\u7a0b\u5e8f\u6dfb\u52a0\u4e00\u4e2a\u53f3\u5927\u62ec\u53f7\uff0c\u4f46\u4e4b\u540e\u5b83\u8868\u73b0\u51fa\u4e0e\u60a8\u76f8\u540c\u7684\u884c\u4e3a\u952f; \u3002\u5b83\u4ece <code>go vet<\/code> \u53d1\u51fa\u4e86\u4e24\u4e2a\u5173\u4e8e\u6ee5\u7528 <code>unsafe.pointer<\/code> \u7684\u8b66\u544a\uff0c\u800c\u6211\u7684\u66f4\u65b0\u7248\u672c\u5219\u6ca1\u6709\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u5982\u4f55\u5728 Go \u6307\u9488\u4e0e uintptr \u4e4b\u95f4\u8fdb\u884c\u8f6c\u6362\u800c\u4e0d\u9020\u6210\u5185\u5b58\u635f\u574f\uff1f\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207560","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207560","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207560"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207560\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}