Home  >  Article  >  Backend Development  >  Is it safe to use unsafe.Pointer to directly convert struct "point" to another struct?

Is it safe to use unsafe.Pointer to directly convert struct "point" to another struct?

WBOY
WBOYforward
2024-02-09 18:48:09416browse

使用 unsafe.Pointer 直接将结构“point”转换为另一个结构是否安全?

In the Go language, you can use `unsafe.Pointer` to directly convert a structure into another structure. However, whether this conversion is safe is a question worthy of discussion. One must be very careful when using `unsafe.Pointer` for struct conversions as it can lead to memory access errors or data corruption. In this case, it can be said that it is unsafe to use `unsafe.Pointer` to directly convert the struct `point` to another struct. Due to the requirement to simplify the text, this issue cannot be discussed in more detail. It is recommended to use this conversion method with caution in actual development and adopt other safe methods to convert structures.

Question content

Is it safe?

(*teamdata)(unsafe.pointer(&team.id))

Sample code:

func testTrans() []*TeamData {
    teams := createTeams()
    teamDatas := make([]*TeamData, 0, len(teams))
    for _, team := range teams {
        // is this safe?
        teamDatas = append(teamDatas, (*TeamData)(unsafe.Pointer(&team.Id)))
    }
    return teamDatas
}

// ??
teams := testTrans()

teams := testtrans() Will the members of the array be garbage collected?

There are many structures and fields returned through grpc, and their definitions are the same as local definitions, so I want to use this more efficient way ((*teamdata)(unsafe.pointer(&team.id))), but I don’t know if there will be any risks.

Complete example: The documentation for https://go.dev/play/p/q3gwp2mervj

WORKAROUND

unsafe.pointer describes supported uses. in particular:

(1) Convert *t1 to a pointer to *t2.

The premise is that t2 is not greater than t1 and the two share one Equivalent memory layout, this transformation allows the data to be reinterpreted One type acts as data for another type.

go's garbage collector is aware of internal pointers and will not collect raw allocations until there are no remaining references to the block. Therefore, larger allocations (grpcretteam in the example) will be pinned when a reference to *teamdata exists.

Another key consideration is the alignment of structure fields. For example:

type Parent struct {
    A uint8
    B uint8
    // 6 bytes of padding to align C.
    C uint64
}

type Bad struct {
    B uint8
    // 7 bytes of padding to align C.
    C uint64
}

In this case, using unsafe to extract bad from parent is invalid because the memory layout is different.

In most cases, it is generally best to avoid the unsafe.pointer trick unless it is needed to meet functional or performance requirements. Code can often be refactored to minimize allocations.

If you must use unsafe to meet performance requirements -- I recommend using the reflect package to implement tests to ensure memory alignment/layout is valid for child structs.

The above is the detailed content of Is it safe to use unsafe.Pointer to directly convert struct "point" to another struct?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete