Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to solve memory resource management problems in C++

How to solve memory resource management problems in C++

Scheduled Pinned Locked Moved C / C++ / MFC
learningc++performancetutorial
16 Posts 4 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 元 Offline
    元 Offline
    元昊 潘
    wrote on last edited by
    #1

    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

    L S 2 Replies Last reply
    0
    • 元 元昊 潘

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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.

      元 1 Reply Last reply
      0
      • L Lost User

        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.

        元 Offline
        元 Offline
        元昊 潘
        wrote on last edited by
        #3

        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

        L 1 Reply Last reply
        0
        • 元 元昊 潘

          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

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          元昊 潘 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.

          元 1 Reply Last reply
          0
          • L Lost User

            元昊 潘 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.

            元 Offline
            元 Offline
            元昊 潘
            wrote on last edited by
            #5

            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.

            L M 2 Replies Last reply
            0
            • 元 元昊 潘

              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.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              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.

              元 1 Reply Last reply
              0
              • L Lost User

                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.

                元 Offline
                元 Offline
                元昊 潘
                wrote on last edited by
                #7

                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.

                L 1 Reply Last reply
                0
                • 元 元昊 潘

                  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.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  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.

                  元 1 Reply Last reply
                  0
                  • L Lost User

                    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.

                    元 Offline
                    元 Offline
                    元昊 潘
                    wrote on last edited by
                    #9

                    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.

                    L 1 Reply Last reply
                    0
                    • 元 元昊 潘

                      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.

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      Very true. And the best way to learn is to take a properly structured course, or buy a good book on the language.

                      元 1 Reply Last reply
                      0
                      • L Lost User

                        Very true. And the best way to learn is to take a properly structured course, or buy a good book on the language.

                        元 Offline
                        元 Offline
                        元昊 潘
                        wrote on last edited by
                        #11

                        OK,I am looking at the C++ Primer Plus of Stephen Prata these days. What else do you recommend? ;P

                        L 1 Reply Last reply
                        0
                        • 元 元昊 潘

                          OK,I am looking at the C++ Primer Plus of Stephen Prata these days. What else do you recommend? ;P

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          Sorry, it is a long time since I read any C++ books, you will need to research for yourself.

                          元 1 Reply Last reply
                          0
                          • L Lost User

                            Sorry, it is a long time since I read any C++ books, you will need to research for yourself.

                            元 Offline
                            元 Offline
                            元昊 潘
                            wrote on last edited by
                            #13

                            Well, I wish myself success in learning programming, thanks,best wishes to you! :-D

                            S 1 Reply Last reply
                            0
                            • 元 元昊 潘

                              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.

                              M Offline
                              M Offline
                              meerokh
                              wrote on last edited by
                              #14

                              You can take a look at the following paper(If you havent already because what you described in second message is what this paper does). Fast-efficient fixed size memory pool[^]

                              1 Reply Last reply
                              0
                              • 元 元昊 潘

                                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

                                S Offline
                                S Offline
                                Stefan_Lang
                                wrote on last edited by
                                #15

                                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)

                                1 Reply Last reply
                                0
                                • 元 元昊 潘

                                  Well, I wish myself success in learning programming, thanks,best wishes to you! :-D

                                  S Offline
                                  S Offline
                                  Stefan_Lang
                                  wrote on last edited by
                                  #16

                                  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)

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups