Skip to content

Commit d806fb9

Browse files
authored
Fix sizeof for non-Array subarrays (#36715)
1 parent 004cb25 commit d806fb9

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

base/subarray.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ size(V::SubArray) = (@_inline_meta; map(n->Int(unsafe_length(n)), axes(V)))
6464

6565
similar(V::SubArray, T::Type, dims::Dims) = similar(V.parent, T, dims)
6666

67-
sizeof(V::SubArray) = length(V) * elsize(V.parent)
67+
sizeof(V::SubArray) = length(V) * sizeof(eltype(V))
68+
sizeof(V::SubArray{<:Any,<:Any,<:Array}) = length(V) * elsize(V.parent)
6869

6970
copy(V::SubArray) = V.parent[V.indices...]
7071

test/subarray.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This file is a part of Julia. License is MIT: https://julialang.org/license
22

3-
using Test, Random
3+
using Test, Random, LinearAlgebra
44

55
######## Utilities ###########
66

@@ -601,6 +601,10 @@ end
601601
arrayOfUInt48 = [a, b, c];
602602

603603
@test sizeof(view(arrayOfUInt48, 1:2)) == 16
604+
605+
@test sizeof(view(Diagonal(zeros(UInt8, 10)), 1:4)) == 4
606+
@test sizeof(view(Diagonal(zeros(UInt8, 10)), 1:3)) == 3
607+
@test sizeof(view(Diagonal(zeros(Float64, 10)), 1:3, 2:6)) == 120
604608
end
605609

606610

0 commit comments

Comments
 (0)