Skip to content

offset_of doesn't correctly calculate offsets of members from nested structs #5851

@avanspector

Description

@avanspector
R1 :: struct {
	_: u16,
	_: int,
	i: int,
}

R2 :: struct {
	_: u16,
	r: R1,
	_: int,
}

main :: proc() {
	r2: R2

	fmt.println(offset_of(R2{}.r.i)) // 16, wrongly assumes offset_of(R1, i)

	fmt.println(offset_of(R2, r) + offset_of(R1, i)) // 24, right

	fmt.println(uintptr(&r2.r.i) - uintptr(&r2)) // // 24, right
}

A fix could imply viability of the offset_of(R2, r.i)/offset_of(R2, "r.i") feature as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions