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

add an example of using sizeof on a struct with padding #47342

Merged
merged 1 commit into from
Nov 3, 2022

Conversation

KristofferC
Copy link
Member

No description provided.

@KristofferC KristofferC added the docs This change adds or pertains to documentation label Oct 27, 2022
Comment on lines +548 to +549
julia> sizeof(StructWithPadding) # not the sum of `sizeof` of fields due to padding
16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keno had proposed changing this (#46322), though we didn't finish

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So is this PR fine or is it an implementation detail of what number this should report when it comes to structs with padding?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exact number here is definitely an implementation detail. For example, on 32-bit platforms this returns:

julia> sizeof(StructWithPadding)
12

@PallHaraldsson
Copy link
Contributor

PallHaraldsson commented Nov 2, 2022

Also mention the order matters for size:

julia> struct StructWithPadding_smaller
         x::Int16
         flag::UInt8
         flag2::Bool
       end

julia> struct StructWithPadding_larger
         flag::UInt8
         x::Int16
         flag2::Bool
       end

@KristofferC
Copy link
Member Author

I think this is an improvement since it makes you aware the sizeof is not necessarily the sum of field sizes.

@KristofferC KristofferC merged commit 65c6bda into master Nov 3, 2022
@KristofferC KristofferC deleted the kc/padding_sizeof branch November 3, 2022 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This change adds or pertains to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants