|
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!
|
|
|
|
|
元昊 潘 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.
|
|
|
|
|
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.
|
|
|
|
|
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?
|
|
|
|
|
Sorry, it is a long time since I read any C++ books, you will need to research for yourself.
|
|
|
|
|
Well, I wish myself success in learning programming, thanks,best wishes to you!
|
|
|
|
|
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)
|
|
|
|
|
|
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)
|
|
|
|
|
Hi,
I am writing a DLL in which certain data have to be shared between all processes, and all threads accessing this DLL. This is put in a Shared section in the DLL. Do the sync objects such as CRITICAL_SECTION, MUTEX, etc. still work as expected? or are there caveats to be weary of. It is something that is very difficult so test or simulate!
Regards,
Bram van Kampen
|
|
|
|
|
Quote: CRITICAL_SECTION, MUTEX, etc. still work as expected? Normally, they do test before releasing the libraries and SDKs, but it still depends on which runtime and its version are you using, as well as what is the deployment OS.
Did you check the documentation? About Synchronization | Microsoft Docs.
Also, please read this reference of synchronization to get to know a bit more about these topics, mutex, critical sections, etc. Synchronization Functions | Microsoft Docs, there are remarks for each function and the type, you can review them. For example, here is the documentation and remarks for the function call that enables the program to enter the critical section, EnterCriticalSection function | Microsoft Docs
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
|
I use this class to print:
Printing Class Library[^]
I can print bitmaps if there are in a separate file. How can I print a bitmap which I added to my
resource. That is, the bitmap is IDB_SAMPLE and I want to use this code
pPage->PrintBitMap(top, left, top + 0.89, left + 0.29, "test.bmp");
where "test.bmp" is LPCSTR variable. How can I convert IDB_SAMPLE which is a bitmap I added to
my resource to a LPCSTR variable so that I can print using this function PrintBitmap().
Please any response any one can give me will be greatly appreciated.
|
|
|
|
|
wrote: I use this class to print:
Printing Class Library[^]
Then why don't you want to ask the author of this article?
|
|
|
|
|
Because he passed away. Im hoping someone can help.
|
|
|
|
|
wrote: Because he passed away.
But did you try to ask him?
Anyway, you should investigate/debug his code to understand how the method CPage::PrintBitMap works.
Then you perhaps will be able to modify or add some code to use the bitmap resource ID rather than the bitmap pathname.
|
|
|
|
|
Victor Nijegorodov wrote: Because he passed away.
But did you try to ask him?
Well, I guess tonight would be the right night to try. Ouija boards at the ready!
NB: "He passed away" means "er ist tot".
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard Deeming wrote: NB: "He passed away" means "er ist tot".
I am sorry...
Please forgive my very poor English...
|
|
|
|