1) Apply for a large amount of memory from the operating system at one time, then build your object on top of it, and then return it to the operating system after use, to avoid frequent new delete overhead and memory fragmentation. These are considerations when your program needs performance very much. 2) When using shared_ptr and unique_ptr, you need to understand who owns the resource. For example, if two Nodes are in the same status, their status is equal. At this time, you cannot know which Node should decide when to release the resources, so neither shared_ptr nor unique_ptr meets the application requirements. From an experience point of view, I suggest you implement it in the way of normal pointers, and finally traverse the entire graph to release resources, so that you can focus on implementing program logic.
The overhead of new and delete memory is relatively large, and it is relatively small to directly find a free memory block from the allocated memory array. So allocate a large memory in advance, divide it into small memory blocks, mark it as free when not in use, and retrieve it faster when in use. This is the prototype of the memory pool. I think simple and radical algorithms are indeed more effective, but pools with complex algorithms are not as good as the operating system's own new delete. After all, the operating system is constantly optimizing memory allocation and release algorithms, but it just chooses the most conservative strategy.
For your second question, the easiest way is to use vector as the main structure of the stack. If you need a more sophisticated implementation, you need to write a space configurator yourself (you can refer to the implementation of the double-layer space configurator in SGI STL)
The one with the word pool is to create a collection, use a certain algorithm on this collection to optimize performance, memory pool, a lot of memory, manage and utilize it yourself. . Thread pool, many threads, management applications, no need to waste time in creation when using it. Timeliness is better
1) Apply for a large amount of memory from the operating system at one time, then build your object on top of it, and then return it to the operating system after use, to avoid frequent new delete overhead and memory fragmentation. These are considerations when your program needs performance very much.
2) When using shared_ptr and unique_ptr, you need to understand who owns the resource. For example, if two Nodes are in the same status, their status is equal. At this time, you cannot know which Node should decide when to release the resources, so neither shared_ptr nor unique_ptr meets the application requirements. From an experience point of view, I suggest you implement it in the way of normal pointers, and finally traverse the entire graph to release resources, so that you can focus on implementing program logic.
The overhead of new and delete memory is relatively large, and it is relatively small to directly find a free memory block from the allocated memory array. So allocate a large memory in advance, divide it into small memory blocks, mark it as free when not in use, and retrieve it faster when in use. This is the prototype of the memory pool. I think simple and radical algorithms are indeed more effective, but pools with complex algorithms are not as good as the operating system's own new delete. After all, the operating system is constantly optimizing memory allocation and release algorithms, but it just chooses the most conservative strategy.
For your second question, the easiest way is to use
vector
as the main structure of the stack. If you need a more sophisticated implementation, you need to write a space configurator yourself (you can refer to the implementation of the double-layer space configurator in SGI STL)The one with the word pool is to create a collection, use a certain algorithm on this collection to optimize performance, memory pool, a lot of memory, manage and utilize it yourself. . Thread pool, many threads, management applications, no need to waste time in creation when using it. Timeliness is better