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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions base/essentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,17 @@ julia> sizeof(1.0)

julia> sizeof(collect(1.0:10.0))
80

julia> struct StructWithPadding
x::Int64
flag::Bool
end

julia> sizeof(StructWithPadding) # not the sum of `sizeof` of fields due to padding
16
Comment on lines +548 to +549
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


julia> sizeof(Int64) + sizeof(Bool) # different from above
9
```

If `DataType` `T` does not have a specific size, an error is thrown.
Expand Down