Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't find struct's some field with pragma pack, 64 bits integer #42761

Closed
hsnks100 opened this issue Nov 21, 2020 · 1 comment
Closed

Can't find struct's some field with pragma pack, 64 bits integer #42761

hsnks100 opened this issue Nov 21, 2020 · 1 comment

Comments

@hsnks100
Copy link

hsnks100 commented Nov 21, 2020

I posted answer on stackoverflow.

https://stackoverflow.com/questions/64945442/cant-find-structs-some-field-with-pragma-pack-64-bits-integer

It may looks like bug. Is it right?

code:

package main

/*
#include <inttypes.h> 
#pragma pack(1)
typedef struct _sss {
uint64_t some; // ok
uint32_t wow; // ok
uint64_t some2; // not found
uint64_t some3; // not found
uint64_t some4; // not found
uint32_t some5; // ok
} Type;
#pragma pack()
*/
import "C"
import (
    "fmt"
)

func main() {
    s := C.Type{}
    s.some = 10; 
    s.wow = 10; 
    s.some2 = 10; 
    s.some5 = 10;
    fmt.Println("Hello, playground")
}

cat _cgo_gotypes.go

...
type _Ctype_struct__sss struct {
	some	_Ctype_uint64_t
	wow	_Ctype_uint32_t
	_	[24]byte
	some5	_Ctype_uint32_t
}
@ianlancetaylor
Copy link
Contributor

Unlike some other projects, we do not use GitHub issues for questions. You will get better and faster answers on a forum.
Please see https://golang.org/wiki/Questions. Thanks.

If the fields of a C struct are aligned such that they can't be represented by a Go struct, then they cannot be accessed directly from cgo. You will have to write functions in C to read and write those fields.

Please take any followups to a separate forum, not here. Thanks.

@golang golang locked and limited conversation to collaborators Nov 21, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants