Skip to content

sizeof returns erroneous results for Union arrays #23321

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

Open
iamed2 opened this issue Aug 18, 2017 · 7 comments
Open

sizeof returns erroneous results for Union arrays #23321

iamed2 opened this issue Aug 18, 2017 · 7 comments
Labels
arrays [a, r, r, a, y, s]

Comments

@iamed2
Copy link
Contributor

iamed2 commented Aug 18, 2017

julia> v = Union{Int,Void}[rand(Bool) ? k : nothing for k = 1:10]
10-element Array{Union{Void, Int64},1}:
   nothing
  2
  3
   nothing
   nothing
   nothing
   nothing
  8
  9
 10

julia> sizeof(v)
1762099360

Based on multiple Julia sessions, the number appears to be some random integer multiplied by the length of the array. A fresh Julia session is more likely to result in 0 so it seems like it's probably accessing the wrong memory somewhere.

@yuyichao
Copy link
Contributor

Caused by #22441 ?

@iamed2
Copy link
Contributor Author

iamed2 commented Aug 18, 2017

Likely, I think

@quinnj
Copy link
Member

quinnj commented Aug 18, 2017

Yeah, there's a few places I need to update sizeof in src; It's updated for Union{Void, Int8} for example, but not for the general object case.

@JeffBezanson
Copy link
Member

This one looks like codegen.cpp:2726. That assumes an array element type stored unboxed is a DataType.

JeffBezanson added a commit that referenced this issue Aug 21, 2017
Ensure sizeof returns correctly for isbits Union arrays. Fixes #23321
@vtjnash vtjnash reopened this Aug 21, 2017
@vtjnash
Copy link
Member

vtjnash commented Aug 21, 2017

since we're defining sizeof to mean eltype_layout, we need to also handle abstract covariant types also (for example, Tuple{Union{Int8, Int16}} == Union{Tuple{Int8}, Tuple{Int16}})

@kshyatt kshyatt added the arrays [a, r, r, a, y, s] label Aug 22, 2017
@laborg
Copy link
Contributor

laborg commented Feb 21, 2022

Can this be closed? #38126

julia> sizeof(Vector{Union{Tuple{UInt8,UInt16}}}(undef,8))
32

julia> sizeof(Vector{Tuple{Union{UInt8,UInt16}}}(undef,4))
32

@vtjnash
Copy link
Member

vtjnash commented Feb 22, 2022

$ ./julia -q
julia> sizeof(Vector{Union{Tuple{UInt8},Tuple{UInt16}}}(undef,4))
8

julia> sizeof(Vector{Tuple{Union{UInt8,UInt16}}}(undef,4))
8

$ ./julia -q
julia> sizeof(Vector{Tuple{Union{UInt8,UInt16}}}(undef,4))
32

julia> sizeof(Vector{Union{Tuple{UInt8},Tuple{UInt16}}}(undef,4))
32

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrays [a, r, r, a, y, s]
Projects
None yet
Development

No branches or pull requests

7 participants