4 ms·
>so it is advised to use pointers to objects on the stack sparingly At least in the C/C++ world, this is not true. Where possible, we prefer stack allocated ob
by madmax96 7y ago
>so it is advised to use pointers to objects on the stack sparingly
At least in the C/C++ world, this is not true. Where possible, we prefer stack allocated objects. Stack allocation is fast and usually gives you better cache performance than heap allocation.
Rust's lifetime/borrow checker provides compiler support for well-established best practices amongst professional C and C++ users.
- bernawil 7y agolooks like you are arguing what "sparingly" means in that context because the rest of the sentence is totally valid. Stack and Heap allocation in C serve different purposes and there's really no rule of thumb to choose one and there's not even one case more frequent than the other. Allocate objects in the stack, it's faster. Unless you need a reference in a function in another context. Or it's too big. Or you're going going to share it between threads. Or whatever.
- IshKebab 7y agoThere's pretty easy rules: stack unless it needs to outlive the current scope, or is very large. That covers 99% of cases.
- bernawil 7y agoBut you realize that "it needs to outlive the current scope" is vague enough to support the statement that there's really no rule of thumb, right?
- randyrand 7y agoSeems pretty objective to me. When the current function returns, will this object ever get used again? Most of the time the answer is no.