-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Description
The documentation for Vec
states:
Do not rely on removed data to be erased for security purposes. Even if you drop a
Vec
, its buffer may simply be reused by anotherVec
.
Here, "its buffer may simply be reused" is simply meant to refer to the behavior of the underlying allocator. But I have seen people with low-level experience be misled by this into thinking that the implementation of Vec
itself has its own global allocation cache. They then thought this cache was responsible for excess memory usage in their programs.
I'm not sure what the best way to clarify this would be. Part of the reason the current wording can be misleading is that "reused by another Vec
" suggests a mechanism specific to Vec
. On the other hand, it would also help to just explicitly mention the allocator. Here is a possible version that addresses both points, but it's more wordy than the current version:
Even if you drop a
Vec
, the allocator may reuse its buffer for anotherVec
or other allocation.
Activity
SkiFire13 commentedon Apr 23, 2021
I guess this also includes in-place iteration?
the8472 commentedon Apr 23, 2021
In-place iteration is a fancy case of type-conversion. Simpler examples are conversions to
IntoIter
,VecDeque
andBinaryHeap
which also keep the allocation.jyn514 commentedon Apr 23, 2021
Maybe it would be more clear to say "may be reused by another allocation"? That doesn't imply it has to be a Vec that reuses the memory.
inquisitivecrystal commentedon Jun 5, 2021
@rustbot label +A-collections +C-enhancement +T-libs
chrismit3s commentedon Sep 30, 2021
@rustbot claim
Clarify a sentence in the documentation of Vec (rust-lang#84488)
Rollup merge of rust-lang#89440 - chrismit3s:issue-84488-fix, r=steve…
Auto merge of rust-lang#89449 - Manishearth:rollup-3alb61f, r=Manishe…
Auto merge of rust-lang#89449 - Manishearth:rollup-3alb61f, r=Manishe…