Answer the question
In order to leave comments, you need to log in
Memory tutorial?
Good afternoon.
Always written in languages that use the garbage collector. Naturally, I never worried about working with memory. But recently I started picking Rust and realized that this gap in knowledge greatly hinders me. I don't know when it will be correct to allocate memory on the heap and when on the stack. How to follow it and how to work with pointers/references correctly.
I want to fill this gap, but I do not know where to look for the material. It is clear that there will be no direct books on this topic, but I would like to find some articles to deal with the issue in detail.
Advise where to look?
Answer the question
In order to leave comments, you need to log in
Start with C++ first as the more common language.
To work, and there were no memory leaks, that is, no longer needed data did not occupy memory.
Clear all dynamically allocated memory when it is no longer needed, roughly speaking, there should be a delete for each new, unless the application terminates immediately, then all memory is cleared ..
They also have memory leaks, but these are special cases, for example, with multithreading.
If possible, allocate on the stack. It is worth allocating on the heap what can be allocated on the stack if the application is highly multi-threaded, not 64-bit, or is used without an MMU.
If you can't allocate on the stack (Rust will report this with ownership errors), then allocate on the heap.
Don't look at C++ - there are too many complexities.
Here are some questions to help you decide how to work with the object.
1. When is the size of an object known? At compile time or at run time?
2. How big is the object? The stack does not have too much memory and everything is not really big, you need to either keep it in memory or work with a file on disk
. To answer the second question, you need to know your user. Absolutely everyone will never be able to write code. Therefore, you need to know in what conditions your code will work.
A week-long necropost, but I suggest using unique_prt, share_ptr, make functions and a std::vector container in C ++ and not worry about cleaning up and allocating memory. Sounds scary, but really easy
C++ without new and delete
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question