# Understanding \`sizeof\` return values on \`Char\` / \`String\`

**URL:** https://discourse.julialang.org/t/understanding-sizeof-return-values-on-char-string/70099
**Category:** General Usage
**Tags:** question, strings, char
**Created:** [October 20, 2021, 2:14pm UTC](https://discourse.julialang.org/t/understanding-sizeof-return-values-on-char-string/70099 "2021-10-20T14:14:56Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![BambOoxX](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bambooxx/32/22179_2.png) [@BambOoxX](https://discourse.julialang.org/u/BambOoxX)
#### Post date: [October 20, 2021, 2:14pm UTC](https://discourse.julialang.org/t/understanding-sizeof-return-values-on-char-string/70099/1 "2021-10-20T14:14:56Z")

</div>

Can someone explain this behavior of `sizeof` vs `summarysize` ?

```julia
sizeof("z")
# 1
sizeof('z')
# 4
Base.summarysize('z')
# 4
Base.summarysize("z")
# 9

```

When I read the doc

> `sizeof(str::AbstractString)`  
> Size, in bytes, of the string `str`. Equal to the number of code units in `str` multiplied by the size, in bytes, of one code unit in `str`.  
> I understand that in this case `sizeof` and `summarysize` should return the same value… What am I missing ?

Some context : I want to convert a Vector of Strings into a Vector of some struct by splitting the strings at some separator, then convert the obtained substrings to more appropriate formats (Char, Int …) if possible.

I am on Julia 1.7.0-rc1

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [October 20, 2021, 2:49pm UTC](https://discourse.julialang.org/t/understanding-sizeof-return-values-on-char-string/70099/2 "2021-10-20T14:49:18Z")

</div>

`sizeof('z') == 4` because a `Char` is stored as a 32-bit value (see `?Char`). This is required so any Unicode codepoint can fit in a Char.

`sizeof("z") == 1` because encoding “z” in UTF-8 takes only one byte.

`Base.summarysize('z') == 4` because a Char is a simple value type.

`Base.summarysize("z") == 9` because… hum I’m not sure: I thing this counts 8 bytes for the pointer to the region of memory that holds the string, and 1 byte for the string itself. But it should also count some bytes for storing the length of the string?

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [October 20, 2021, 2:56pm UTC](https://discourse.julialang.org/t/understanding-sizeof-return-values-on-char-string/70099/3 "2021-10-20T14:56:09Z")

</div>

> [@BambOoxX](#):
>
> ```julia
> Base.summarysize("z")
> # 9
> 
> ```

The reason that this is `9` is that, internally, a `String` consists both of an array of bytes (UTF-8 code units for the encoded string) and an internal `length::Int` field and `summarysize` [includes the `Int` size](https://github.com/JuliaLang/julia/blob/05515f4a727028be0772e15e9885173b6678ffdb/base/summarysize.jl#L105-L109). `sizeof(Int) == 8` on a 64-bit machine, and `1+8 == 9`. ([Technically](https://github.com/JuliaLang/julia/blob/05515f4a727028be0772e15e9885173b6678ffdb/src/array.c#L520-L546), a `String` object may have an even bigger footprint in memory: not only may it implicitly include a 1-byte NUL terminator for ease of passing to C, but a heap-allocated Julia value can also have a [preamble](https://github.com/JuliaLang/julia/blob/05515f4a727028be0772e15e9885173b6678ffdb/src/julia.h#L88-L106) with a type tag and some other info.) In contrast, `sizeof` only gives you the size of the underlying `String` data and not the Julia wrappers thereof.
