簡體   English   中英

Golang-結構,初始化和memcpy-正確的方法是什么?

[英]Golang - structs, initializing and memcpy - what is the proper way?

我是Go的新手,我希望翻譯一些我必須去的C / C ++代碼,但我還沒有做到。 問題存在兩個地方:如何初始化定義的結構以及如何執行“內存復制”

我正在談論的代碼是這樣的: 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 data

Go代碼如下所示:

            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);

謝謝!

因此,基本上給出了以下基本類型:

type xchr int8
type xint int
type xdob float64
type xflt float32

您要復制以下struct類型的值的字節(內存表示形式):

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
}

請注意,Go中int的長度取決於平台,取決於您編譯到的目標體系結構,它的長度可以是32位或64位。 這將導致平台相關的行為,所以現在讓我們將其修復為int32

type xint int32

這就是上述struct的字節大小的計算方式。 如果需要int64 ,只需對其進行更改並在大小計算中添加4個額外的字節。

接下來,您需要將結果放入元素類型為xchr的數組中。 您需要一個足夠大的數組,即“前綴”為"VEH1"后跟上述struct的數據。 因此它的大小必須為4+sizeof(VEH1) ,即56

在“將一種類型的字節復制到另一種類型的內存空間”中,您可以使用unsafe包及其通用Pointer類型來完成此操作。 任何指針都可以unsafe.Pointer unsafe.Pointerunsafe.Pointerunsafe.Pointer可以unsafe.Pointer unsafe.Pointer為任何指針類型,因此這是您在不同指針類型之間的“網關”。

您可以采用structVHE1的地址,將其轉換為Pointer ,然后將結果Pointer轉換為Pointer目標數據類型的指針。 取消引用指針,現在您已經具有其他類型的值。

內置的copy()只能與sli​​ce一起使用,因此首先您需要對數據數組進行切片,以便將它們傳遞給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)

包裝成適合屏幕的輸出:

{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上嘗試運行的演示。

結果為[]byte

如果您想要[]byte結果(例如,您要將結果寫入io.Writer ),則對data_send使用[56]byte類型,當然將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[:])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM