{"id":41408,"date":"2024-12-01T13:47:02","date_gmt":"2024-12-01T05:47:02","guid":{"rendered":"https:\/\/fwq.ai\/blog\/41408\/"},"modified":"2024-12-01T13:47:02","modified_gmt":"2024-12-01T05:47:02","slug":"%e5%bd%93-cmatrix-%e8%bd%ac%e6%8d%a2%e4%b8%ba%e5%88%87%e7%89%87%e6%97%b6%ef%bc%8cgo-vet-%e6%8a%b1%e6%80%a8-unsafeptr","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/41408\/","title":{"rendered":"\u5f53 CMatrix \u8f6c\u6362\u4e3a\u5207\u7247\u65f6\uff0cGo-vet \u62b1\u6028 unsafeptr"},"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>\u5f53 CMatrix \u8f6c\u6362\u4e3a\u5207\u7247\u65f6\uff0cGo-vet \u62b1\u6028 unsafeptr<\/span><\/p>\n<h1>\u5f53 CMatrix \u8f6c\u6362\u4e3a\u5207\u7247\u65f6\uff0cGo-vet \u62b1\u6028 unsafeptr<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-20 08:36:36<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5f53 CMatrix \u8f6c\u6362\u4e3a\u5207\u7247\u65f6\uff0cGo-vet \u62b1\u6028 unsafeptr\u300b<\/span>\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u4f7f\u7528 opencv \u548c go \u5e76\u5177\u6709\u6b64\u529f\u80fd\uff1a<\/p>\n<pre>func convertmattoslice(c_mat *cmatrix) []float32 {\n    c_output := c.convert(c_mat.ctype)\n    h := &amp;reflect.sliceheader{\n        data: uintptr(unsafe.pointer(c_output.val)),\n        len:  int(c_output.length),\n        cap:  int(c_output.length),\n    }\n    return *(*[]float32)(unsafe.pointer(h))\n}<\/pre>\n<p>\u5f53\u6211\u8fd0\u884c <code>go vet .\/...<\/code> \u65f6\uff0c\u5b83\u7ed9\u4e86\u6211\uff1a<\/p>\n<pre>opencv_wrapper.go:31:7: unsafeptr: possible misuse of reflect.SliceHeader (govet)\n    h := &amp;reflect.SliceHeader{\n         ^<\/pre>\n<p>\u6709\u6ca1\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u5f97\u5230\u5207\u7247\uff0c\u56e0\u4e3a\u6211\u786e\u4fe1 go-vet \u6709\u7406\u7531\u62b1\u6028\u8fd9\u4e00\u70b9\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u8bf7\u53c2\u9605 \u3002\u66f4\u5b89\u5168\u7684\u66ff\u4ee3\u65b9\u6848\u5982\u4e0b\uff1a<\/p>\n<pre>func convertmattoslice(c_mat *cmatrix) []float32 {\n    c_output := c.convert(c_mat.ctype)\n    result := make([]float32, c_output.length)\n    sh := (*reflect.sliceheader)(unsafe.pointer(&amp;result))\n    sh.data = uintptr(unsafe.pointer(c_output.val))\n    return result\n}<\/pre>\n<p>\u6211\u5728\u4e0d\u4f7f\u7528 cgo \u7684\u60c5\u51b5\u4e0b\u5236\u4f5c\u4e86\u4e00\u4e2a  \u6765\u6a21\u62df\u60a8\u4ee3\u7801\u7684\u884c\u4e3a\u3002\u56e0\u6b64\uff0c\u8ba9\u6211\u4eec\u5c06\u5176\u653e\u5728\u4e00\u4e2a\u76ee\u5f55\u4e2d\uff0c\u5e76\u4f7f\u7528 <code>go build -gcflags \"-m -m\"<\/code> \u8fdb\u884c\u4e00\u4e9b\u9003\u9038\u5206\u6790\u3002<\/p>\n<p>\u6211\u4eec\u611f\u5174\u8da3\u7684\u662f\u786e\u4fdd\u6211\u4eec\u8fd4\u56de\u7684\u5207\u7247\uff08\u5207\u7247\u5934\uff09\u5206\u914d\u5728\u5806\u4e0a\uff0c\u5e76\u4e14\u5b83\u6307\u5411 <code>data<\/code> \u5b57\u6bb5\u4e2d\u7684\u6307\u9488\u4e5f\u662f\u5982\u6b64\u3002\u4ece\u9003\u9038\u5206\u6790\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u5230\uff0c<code>b<\/code> \u9003\u9038\u4e86\uff0c\u5207\u7247\u5206\u914d\u4e5f\u9003\u9038\u4e86\uff1a<\/p>\n<pre>.\/main.go:10:6: b escapes to heap:\n.\/main.go:10:6:   flow: {storage for &amp;output literal} = &amp;b:\n.\/main.go:10:6:     from &amp;b (address-of) at .\/main.go:17:38\n[...]\n.\/main.go:25:16: make([]float32, c_output.length) escapes to heap:\n.\/main.go:25:16:   flow: {heap} = &amp;{storage for make([]float32, c_output.length)}:\n.\/main.go:25:16:     from make([]float32, c_output.length) (non-constant size) at .\/main.go:25:16<\/pre>\n<p>\u5c31\u60a8\u800c\u8a00\uff0c\u60a8\u5fc5\u987b\u4ece c \u4ee3\u7801\u4e2d\u786e\u4fdd\u6307\u9488\u5df2\u5206\u914d\u5728\u5806\u4e0a\u3002<\/p>\n<p>\u4e00\u822c\u6765\u8bf4\uff0c\u4ece\u6211\u5f15\u7528\u7684 go \u95ee\u9898\u7684\u8ba8\u8bba\u4e2d\u53ef\u4ee5\u5f97\u51fa\u7684\u89c4\u5219\u662f\u6c38\u8fdc\u4e0d\u8981\u81ea\u5df1\u521b\u5efa sliceheaders\u3002\u5b83\u4eec\u53ef\u80fd\u4f1a\u8d77\u4f5c\u7528\uff0c\u4f46\u5b83\u4eec\u53ef\u80fd\u5e76\u4e14\u5c06\u4f1a\u5bfc\u81f4\u5185\u5b58\u635f\u574f\uff0c\u4ece\u800c\u5728\u4ee3\u7801\u4e2d\u9020\u6210\u56f0\u96be\u4e14\u96be\u4ee5\u8c03\u8bd5\u7684\u60c5\u51b5\u3002\u5982\u679c\u60a8\u786e\u5b9e\u9700\u8981\u81ea\u5df1\u8bbf\u95ee\u5207\u7247\uff0c\u8bf7\u4f7f\u7528 <code>make<\/code> \u6216\u4f7f\u7528  \u6765\u6267\u884c\u6b64\u64cd\u4f5c\uff0c\u5b83\u4eec\u4fdd\u8bc1\u53ef\u4ee5\u6b63\u5e38\u5de5\u4f5c\u5e76\u4e0e gc \u6b63\u786e\u914d\u5408\uff0c\u7136\u540e\u5bf9\u5207\u7247\u6807\u5934\u8fdb\u884c\u5fc5\u8981\u7684\u66f4\u6539\u3002<\/p>\n<p>\u5728 go 1.21 \u4e2d\uff0creflect.sliceheader \u6807\u5934\u7684\u4f7f\u7528\u5df2\u88ab\u5e9f\u5f03\u3002\u73b0\u5728\u6267\u884c\u7c7b\u4f3c\u64cd\u4f5c\u7684\u66ff\u4ee3\u4ee3\u7801\u5982\u4e0b\u6240\u793a<\/p>\n<pre>func ConvertMatToSlice(c_Mat *CMatrix) []float32 {\n    c_output := C.Convert(c_Mat.CType)\n    sh := unsafe.Slice(c_output.val, c_output.length)\n    return *(*[]float32)(unsafe.Pointer(&amp;sh))\n}\n<\/pre>\n<p>\u4e4b\u524d\u56de\u7b54\u4e2d\u6240\u8bf4\u7684\u4e00\u5207\u4ecd\u7136\u6709\u6548\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u5f53 CMatrix \u8f6c\u6362\u4e3a\u5207\u7247\u65f6\uff0cGo-vet \u62b1\u6028 unsafeptr\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u7c73\u4e91\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03Golang\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; \u5f53 CMatrix \u8f6c\u6362\u4e3a\u5207\u7247\u65f6\uff0cGo-vet \u62b1\u6028 unsafeptr \u5f53 CMatrix \u8f6c\u6362\u4e3a\u5207\u7247\u65f6\uff0cGo-vet \u62b1\u6028 unsafeptr \u6765\u6e90\uff1astackoverflow 2024-04-20 08:36:36 0\u6d4f\u89c8 \u6536\u85cf \u4eca\u5929\u7c73\u4e91\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86\u300a\u5f53 CMatrix \u8f6c\u6362\u4e3a\u5207\u7247\u65f6\uff0cGo-vet \u62b1\u6028 unsafeptr\u300b\uff0c\u5176\u4e2d\u6d89\u53ca\u5230\u7684\u77e5\u8bc6\u70b9\u5305\u62ec\u7b49\u7b49\uff0c\u65e0\u8bba\u4f60\u662f\u5c0f\u767d\u8fd8\u662f\u8001\u624b\uff0c\u90fd\u9002\u5408\u770b\u4e00\u770b\u54e6~\u6709\u597d\u7684\u5efa\u8bae\u4e5f\u6b22\u8fce\u5927\u5bb6\u5728\u8bc4\u8bba\u7559\u8a00\uff0c\u82e5\u662f\u770b\u5b8c\u6709\u6240\u6536\u83b7\uff0c\u4e5f\u5e0c\u671b\u5927\u5bb6\u80fd\u591a\u591a\u70b9\u8d5e\u652f\u6301\u5440\uff01\u4e00\u8d77\u52a0\u6cb9\u5b66\u4e60~ \u95ee\u9898\u5185\u5bb9 \u6211\u6b63\u5728\u4f7f\u7528 opencv \u548c go \u5e76\u5177\u6709\u6b64\u529f\u80fd\uff1a func convertmattoslice(c_mat *cmatrix) []float32 { c_output := c.convert(c_mat.ctype) h := &amp;reflect.sliceheader{ data: uintptr(unsafe.pointer(c_output.val)), len: int(c_output.length), cap: int(c_output.length), } return *(*[]float32)(unsafe.pointer(h)) } \u5f53\u6211\u8fd0\u884c go vet [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-41408","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41408","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=41408"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/41408\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=41408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=41408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=41408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}