第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

Golang - 結(jié)構(gòu)、初始化和 memcpy - 正確的方法是什么?

Golang - 結(jié)構(gòu)、初始化和 memcpy - 正確的方法是什么?

Go
縹緲止盈 2021-10-18 10:31:51
我是 Go 的新手,我希望翻譯一些我必須去的 C/C++ 代碼,但我一直沒能做到。問題存在于 2 個(gè)地方:如何初始化我定義的結(jié)構(gòu)以及如何執(zhí)行“memcopy”我正在談?wù)摰拇a是這樣的:http : //play.golang.org/p/e8N255qEAk 第 69 和 74 行。我想“翻譯”到 Go 的 C/C++ 代碼是這樣的:            typedef char xchr;            typedef int xint;            typedef double xdob;            typedef float xflt;            typedef struct            {               xint p;                xdob lat_lon_ele[3];               xflt psi_the_phi[3];               xflt gear_flap_vect[3];            }VEH1;            avio.p = 0;            avio.lat_lon_ele[0] = 47.460058;            avio.lat_lon_ele[1] = -122.32104;            avio.lat_lon_ele[2] = 8000.000;            avio.psi_the_phi[0] = 110.5;            avio.psi_the_phi[1] = 0.0;            avio.psi_the_phi[2] = 0.0;            avio.gear_flap_vect[0] = 1.0;            avio.gear_flap_vect[1] = 0.0;            avio.gear_flap_vect[2] = 0.0;            VEH1* av = &avio;            xchr data_send[6];            data_send[0]='V';            data_send[1]='E';            data_send[2]='H';            data_send[3]='1';            data_send[4]=0;            memcpy(&data_send[5],av,sizeof(VEH1)); // load in the dataGo 代碼如下所示:            type xchr int8            type xint int            type xdob float64            type xflt float32            type VEH1 struct {                p              xint                lat_lon_ele    [3]xdob                psi_the_phi    [3]xflt                gear_flap_vect [3]xflt            }            type VEHA struct {                num_p xint                lat_lon_ele    [10][3]xdob                psi_the_phi    [10][3]xflt                gear_flap_vect [10][3]xflt                lat_view, lon_view, ele_view xdob                psi_view, the_view, phi_view xflt            }            var avio VEH1            avio = &VEH1{0, {47.460058, -122.32104, 8000.000}, {110.5, 0.0, 0.0}, {1.0, 0.0, 0.0}}            data_send := [6]xchr{'V', 'E', 'H', '1', 0, 0}            copy(data_send[5:5], avio);謝謝!
查看完整描述

1 回答

?
森林海

TA貢獻(xiàn)2011條經(jīng)驗(yàn) 獲得超2個(gè)贊

所以基本上給出了這些基本類型:


type xchr int8

type xint int

type xdob float64

type xflt float32

您想要復(fù)制以下struct類型值的字節(jié)(內(nèi)存表示):


type VEH1 struct { // 52 bytes total

    p              xint    // 4 bytes (READ BELOW)

    lat_lon_ele    [3]xdob // 24 bytes

    psi_the_phi    [3]xflt // 12 bytes

    gear_flap_vect [3]xflt // 12 bytes

}

請注意,intGo中的長度取決于平臺,它可能是 32 位或 64 位,具體取決于您編譯到的目標(biāo)架構(gòu)。這將導(dǎo)致平臺相關(guān)行為,所以讓我們暫時(shí)將其修復(fù)int32為:


type xint int32

這就是上述字節(jié)大小的struct計(jì)算方式。如果需要int64,只需更改它并在大小計(jì)算中添加 4 個(gè)額外字節(jié)。


接下來,您需要元素類型為 的數(shù)組中的結(jié)果xchr。您需要一個(gè)足夠大的數(shù)組,即“前綴”"VEH1"后跟上面的數(shù)據(jù)struct。所以它的大小必須4+sizeof(VEH1)是56。


在 Go 中將一種類型的字節(jié)復(fù)制到另一種類型的內(nèi)存空間中,可以將其unsafe封裝及其通用Pointer類型。任何指針都可以轉(zhuǎn)換為unsafe.Pointer并且unsafe.Pointer可以轉(zhuǎn)換為任何指針類型,因此這是不同指針類型之間的“網(wǎng)關(guān)”。


您可以獲取 的struct值的地址VHE1,將其轉(zhuǎn)換為Pointer,并將結(jié)果轉(zhuǎn)換Pointer為指向目標(biāo)數(shù)據(jù)類型的指針。取消引用指針,現(xiàn)在您已經(jīng)擁有其他類型的值。


內(nèi)置copy()函數(shù)只能與切片一起使用,因此首先您需要對數(shù)據(jù)數(shù)組進(jìn)行切片以便能夠?qū)⑺鼈儌鬟f給copy().


你可以這樣做:


avio := VEH1{0, [3]xdob{47.460058, -122.32104, 8000.000},

    [3]xflt{110.5, 0.0, 0.0}, [3]xflt{1.0, 0.0, 0.0}}

fmt.Printf("%+v\n", avio)


pavio := unsafe.Pointer(&avio)

pavio_arr := *((*[52]xchr)(pavio))


data_send := [56]xchr{'V', 'E', 'H', '1'}

n := copy(data_send[4:], pavio_arr[:])

fmt.Printf("Copied %d bytes\n", n)


fmt.Printf("%+v\n", data_send)

輸出,包裝以適應(yīng)屏幕:


{p:0 lat_lon_ele:[47.460058 -122.32104 8000] psi_the_phi:[110.5 0 0]

    gear_flap_vect:[1 0 0]}

Copied 52 bytes

[86 69 72 49 0 0 0 0 0 0 0 0 -81 33 56 46 -29 -70 71 64 77 45 91 -21 -117

    -108 94 -64 0 0 0 0 0 64 -65 64 0 0 -35 66 0 0 0 0 0 0 0 0 0 0 -128 63 0 0 0 0]

在Go Playground上嘗試工作演示。


結(jié)果為 []byte

如果您想要[]byte結(jié)果(例如,您想將結(jié)果寫入 an io.Writer),則使用[56]byte類型 fordata_send并且當(dāng)然強(qiáng)制轉(zhuǎn)換pavio為*[52]byte:


pavio := unsafe.Pointer(&avio)

pavio_arr := *((*[52]byte)(pavio))


data_send := [56]byte{'V', 'E', 'H', '1'}

n := copy(data_send[4:], pavio_arr[:])


查看完整回答
反對 回復(fù) 2021-10-18
  • 1 回答
  • 0 關(guān)注
  • 329 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號