Legal values for a Box<T> where T is zero-sized
See original GitHub issueOn IRC, @mystor asked me whether, given some zero-sized type ZST
, it would be legal to represent Box<ZST>
using some arbitrary non-null value, or must it be a particular value (e.g., 1)?
For example, if there is a pointer p
to some stack variable, can one write unsafe { Box::from_raw(p as *mut ZST) }
and have that be a legal box?
Issue Analytics
- State:
- Created 7 years ago
- Comments:18 (4 by maintainers)
Top Results From Across the Web
std::boxed
For zero-sized values, the Box pointer still has to be valid for reads and writes and sufficiently aligned. In particular, casting any aligned...
Read more >Econ 388 Final Flashcards
expected value is equal to the true value, bias and variance approach 0 as sample size increases, has the smallest variance out of...
Read more >Untitled
Conscious value real estate, Red rocket tattoo facebook, Gseat gel seat cushion ... Rwanda money laundering law, Walk don't run drum cover, Warners...
Read more >rust - How can I store a type in an array?
We use zero sized types (ZST) for the builders, because we have nothing to store there. The main function is also quite straightforward....
Read more >Untitled
Pfeil nach rechts symbol, 1977 buick regal value, What does 2x platinum mean, ... Art jargon.tumblr, 5 great photographers, Java zero length byte...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’m of the opinion that
Box::from_raw
should only ever be called with a pointer allocated byBox::new
.Conceptually, we are allocating a zero-sized block of memory (which may have malloc headers, so the real allocation may be bigger than zero), and we should use the same pointer value when we are freeing it.
The fact that
Box::new
currently always returns a pointer value of 1 is an implementation detail: it’s just an optimization to avoid allocating zero-sized blocks of memory.No 😦 We haven’t really started writing down anything yet… or even found a good structure to put stuff in. I hope to be able to work on that some more this summer.