How to solve memory resource management problems in C++
-
Hi I am a C++ beginner and I have looked up a few articles on memory management,but most authors think autoptr should be used. However, I am not familiar with the library of them, and I need your guidance that I want to use linked lists to link to memory pools. Thank you! :)
xgzsChris
-
Hi I am a C++ beginner and I have looked up a few articles on memory management,but most authors think autoptr should be used. However, I am not familiar with the library of them, and I need your guidance that I want to use linked lists to link to memory pools. Thank you! :)
xgzsChris
-
You need to provide more information. The statement "I want to use linked lists to link to memory pools" does not really make it clear what you are trying to do.
When started, a "Memory Pool" allocates a large chunk of Memory and will split the chunk into smaller chunks,such as 100 bytes.Every time you request memory space from a memory pool, it gets chunks that have been allocated previously, not from the operating system. Though I have such a idea,but I have trouble coming it true. I'll appreciate it if you could give me some advice or share code after abstraction. Maybe my request will take you a long time. Thank you very much! ;P
-
When started, a "Memory Pool" allocates a large chunk of Memory and will split the chunk into smaller chunks,such as 100 bytes.Every time you request memory space from a memory pool, it gets chunks that have been allocated previously, not from the operating system. Though I have such a idea,but I have trouble coming it true. I'll appreciate it if you could give me some advice or share code after abstraction. Maybe my request will take you a long time. Thank you very much! ;P
-
元昊 潘 wrote:
Maybe my request will take you a long time.
Well it would if I was going to try it. But I wonder what actual use this is when the C/C++ systems have a perfectly reasonable set of functions to manage memory already.
A common problem with using C++ in embedded systems is memory allocation, which is a loss of control over the new and delete operators. Ironically, the root of the problem is that C++ 's memory management is very easy and secure.Specifically, when an object is eliminated, its destructor can safely free the allocated memory. This is of course a good thing, but the simplicity of this use makes programmers overuse new and delete without paying attention to cause and effect in an embedded C++ environment.Furthermore, in embedded systems, frequent dynamic allocation of memory of variable size can cause significant problems and risk of heap fragmentation due to memory limitations. As a warning, conservative use of memory allocation is the first principle in an embedded environment. But when you have to use new and delete, you have to control memory allocation in C++.You need to replace the system's memory allocation with a global new and delete, and overloads new and delete from class to class. It maybe also a good solution by operating new and delete,but I think that linking memory blocks with linked lists can be a relatively simple approach, as my teacher said.
-
A common problem with using C++ in embedded systems is memory allocation, which is a loss of control over the new and delete operators. Ironically, the root of the problem is that C++ 's memory management is very easy and secure.Specifically, when an object is eliminated, its destructor can safely free the allocated memory. This is of course a good thing, but the simplicity of this use makes programmers overuse new and delete without paying attention to cause and effect in an embedded C++ environment.Furthermore, in embedded systems, frequent dynamic allocation of memory of variable size can cause significant problems and risk of heap fragmentation due to memory limitations. As a warning, conservative use of memory allocation is the first principle in an embedded environment. But when you have to use new and delete, you have to control memory allocation in C++.You need to replace the system's memory allocation with a global new and delete, and overloads new and delete from class to class. It maybe also a good solution by operating new and delete,but I think that linking memory blocks with linked lists can be a relatively simple approach, as my teacher said.
-
Well you started off by saying you are a C++ beginner, and now you are talking about issues with embedded systems. So I am a little confused as to your actual experience and knowledge levels, and what this question is really all about.
Well,to tell you the truth,I have just been studying programming for a year,and only learned C and C++.Some of the content in the last post came from articles I read.I've learned about almost three solutions, and it turns out that I think linked list solution is easier, and maybe you haven't heard of it because I've looked through many articles and most of the ones they introduced are the autoptr classes.As long as you can express your opinion, it will be of great help to me.
-
Well,to tell you the truth,I have just been studying programming for a year,and only learned C and C++.Some of the content in the last post came from articles I read.I've learned about almost three solutions, and it turns out that I think linked list solution is easier, and maybe you haven't heard of it because I've looked through many articles and most of the ones they introduced are the autoptr classes.As long as you can express your opinion, it will be of great help to me.
The auto_ptr (like most constructs) is just a mechanism that eases the work required to make some piece of code more efficient, or easier to write. It is not a universal solution to every problem. So whatever you use it for you need first to be sure it is the right thing to use: see Smart Pointers (Modern C++)[^] for some more information.
-
The auto_ptr (like most constructs) is just a mechanism that eases the work required to make some piece of code more efficient, or easier to write. It is not a universal solution to every problem. So whatever you use it for you need first to be sure it is the right thing to use: see Smart Pointers (Modern C++)[^] for some more information.
-
Thank you very much!By the way, if I want to learn programming well, I need to understand the role and usage of most library functions.
-
Very true. And the best way to learn is to take a properly structured course, or buy a good book on the language.
-
OK,I am looking at the C++ Primer Plus of Stephen Prata these days. What else do you recommend? ;P
-
Sorry, it is a long time since I read any C++ books, you will need to research for yourself.
-
A common problem with using C++ in embedded systems is memory allocation, which is a loss of control over the new and delete operators. Ironically, the root of the problem is that C++ 's memory management is very easy and secure.Specifically, when an object is eliminated, its destructor can safely free the allocated memory. This is of course a good thing, but the simplicity of this use makes programmers overuse new and delete without paying attention to cause and effect in an embedded C++ environment.Furthermore, in embedded systems, frequent dynamic allocation of memory of variable size can cause significant problems and risk of heap fragmentation due to memory limitations. As a warning, conservative use of memory allocation is the first principle in an embedded environment. But when you have to use new and delete, you have to control memory allocation in C++.You need to replace the system's memory allocation with a global new and delete, and overloads new and delete from class to class. It maybe also a good solution by operating new and delete,but I think that linking memory blocks with linked lists can be a relatively simple approach, as my teacher said.
-
Hi I am a C++ beginner and I have looked up a few articles on memory management,but most authors think autoptr should be used. However, I am not familiar with the library of them, and I need your guidance that I want to use linked lists to link to memory pools. Thank you! :)
xgzsChris
I am referring to the added information in your other responses, specifically your goal to implement a memory pool. First, it seems to me you've set your goal prematurely: You're worrying about the performance overhead of overusing new/delete, but I'm not at all sure that this will really be relevant in your environment. - For one, your application may be fast enough as it is. Any time spent on optimization is wasted. - Second, even if it is slow, memory allocation may only eat ~10% of the total processing time. therefore even a 100-fold speedup can not increase your app speed by more than 10%. - Third, even if the amount of time your app spends on memory management is really excessive, the problem is most likely the programming that requires so much allocations and deallocations, not the speed of each individual allocation. You can very likely improve the speed much more by fixing that programming rather than fixing your memory handling. - Fourth, the systems memory management functions are already very efficient. Doing a billion allocations and deallocations takes only a few seconds. It's not so easy to beat that without a very thorough understanding of memory management as well as a good understanding of the memory dynamics of your application. And if you do understand what your app needs, you should be able to come up with a program design that doesn't lean so heavily on memory management. All that said, if you still want a memory manager, why not use what you can already find on the web? For instance here is a neat memory manager that is already implemented as an allocator for using it with STL containers: GitHub - foonathan/memory: STL compatible C++ memory allocator library using a new RawAllocator concept that is similar to an Allocator but easier to use and write.[^] . Being an allocator means you can just use STL containers rather than custom containers and don't need to care about explicitely allocating or deallocating individual elements.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
-
While, like Richard, I haven't read any (recent) books on C++, The C++ programming Language[^] is still pretty much a must-have. Bjarne Stroustrup keeps updating it to make sure it covers all the new features of the constantly evolving C++ standard (currently C++17, working on C++20). And it provides lots of useful examples that help understand all the great features of this language. Other than that, I suggest searching the web for articles from specific Book authors such as Herb Sutter (check out his Guru of the Week series[^] !) , Alexei Alexandrescu, or Scott Hanselman.
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)