Module show_notes::e005
source · Expand description
Allocate it where?
- Date: November 8, 2015
- Subject: Returning functions from other functions, and thinking about the stack, the heap, and reference types.
- Audio
§Notes
This episode, we look at returning functions from other functions, and as part of that discuss some basics about the stack and the heap—and why we need to care about them for returning functions.
The functions themselves are not especially interesting; they just show you the basic form you use to return functions from other functions, and how to then use them in another function. You’ll want to take a detailed look instead at the documentation for each (or just read the source!), because that’s where the meat of the discussion in this week’s code is.
§Corrigenda
-
Steve Klabnik pointed out that my description of
Vector
types as totally heap-allocated was misleading. It’s better to say that the contents of theVector
– its data – is heap-allocated, while the memory for the smart pointer and associated metadata are allocated on the stack. I had this in mind, and sort of alluded to it earlier in the discussion, but the way I actually said it was misleading at best. -
Chad Sharp (@crossroads1112 on GitHub) clarified that in C99, C does support dynamic array allocation, though it became optional for compilers to implement it as of C11. I forgot about this because I spend so much of my time dealing with Visual C++, which does not support dynamic array allocation. (Notably, Visual C does; it is Visual C++ which does not: remember, C and C++ are related but distinct languages.)
Thanks to Steve and Chad for their helpful feedback!
§Links
- Rust 1.4 release announcement
- “Clarify (and improve) rules for projections and well-formedness”
- MSVC support tracking issue
- Rust 1.4 full release notes
- “What and where are the stack and the heap?”
§Sponsors
- reddraggone9
- Chris Patti
§Become a sponsor
§Follow
- New Rustacean:
- Twitter: @newrustacean
- App.net: @newrustacean
- Email: hello@newrustacean.com
- Twitter: @newrustacean
- Chris Krycho
- Twitter: @chriskrycho
- App.net: @chriskrycho
- Twitter: @chriskrycho
Functions§
- Uses the
doubler_factory
to get a function which doubles a number. - Creates a closure which doubles an integer.
- Creates a function which doubles an integer.