Explore boost pool library learning notes and boostpool learning notes

Source: Internet
Author: User

Explore boost pool library learning notes and boostpool learning notes
Pool

 

 

 

Memory Pool Overview

 

Normally, we are used to directly use APIs such as new and malloc to apply for memory allocation. The disadvantage of this is that the size of the applied memory block is not fixed, frequent use may cause a large amount of memory fragments and thus reduce performance.

 

The memory pool applies to allocate a certain amount of memory blocks with the same size (normally) for backup before actually using the memory. When there is a new memory demand, a part of the memory block will be split from the memory pool. If the memory block is insufficient, continue to apply for a new memory. A significant advantage of this is to avoid Memory fragments as much as possible, improving the memory allocation efficiency.

 

 

 

Pool library Overview

 

The pool library implements a fast and compact memory pool library, which not only manages a large number of objects, but also serves as the STL memory distributor, to some extent, it is similar to a small garbage collection mechanism. It is highly efficient when a large number of small objects need to be allocated and released, and there is no need to consider delete.

 

The pool library includes four components: pool, object_pool, singleton_pool, and pool_alloc.

 

 

 

 

Pool

 

The simplest and easiest to use memory pool returns a memory pointer of the simple data type.

In the header file # include <boost/pool. hpp>

 

 

Operation Functions

 

 

 

 

 

Usage

 

 

# Include <iostream> # include <boost/pool. hpp> using namespace std; using namespace boost; int main () {pool <> rpool (sizeof (int )); // defines the memory block size of the memory pool in int * p = (int *) rpool. malloc (); // void * to int * rpool. free (p); // release return 0 ;}


Note:

The pool does not throw an exception when the allocation fails. When writing the code, check whether the returned result is empty.

 

 

 

Object_pool

 

Object_pool is used for the memory pool of objects (class instances.

Include header file # include <boost/pool/object_pool.hpp>

 

 

Operation Functions

 

 

 

Usage

 

 

 

# Include <iostream> # include <boost/pool/object_pool.hpp> using namespace std; using namespace boost; class class_type {public: int a; class_type (int _ a = 0 ): a (_ a) {};}; int main () {object_pool <class_type> pl; // defines the memory pool class_type * p = pl for this class. malloc (); // call the malloc function to return a class type memory block to initialize this type of pointer cout <p-> a; // The default value is 0 p = pl. construct (100); // construct a new object cout <p-> a; return 0 ;}


 

 

Singleton_pool

 

Header file # include <boost/pool/singleton_pool.hpp>

Consistent with the pool Interface

 

 

 

 

Pool_alloc

 

Header file # include <boost/pool/pool_alloc.hpp>

Provides two standard container template parameters: pool_alloc and fast_pool_allocator.

 

Usage

 

 

#include<iostream>#include<boost/pool/pool_alloc.hpp>#include<vector>using namespace std;using namespace boost;int main(){    vector<int,pool_allocator<int> >  v;    v.push_back(1);    cout<<v.size();    return 0;}


 

 

 

 

 


BOOST library usage Problems

Copy all the compiled. lib files to vs2010.
For example
C: \ Program Files \ Microsoft Visual Studio 10.0 \ VC \ include \ has a boost folder, which contains useful header Files

C: \ Program Files \ Microsoft Visual Studio 10.0 \ VC \ lib has your. lib File

Otherwise, the vc cannot find the include and link paths.
That's what I did ......

What are the main functions of the boost library? Some improvements in c ++ after this library is available

The boost library contains all the basic data structures and frameworks that can be thought. In this case, boost is used as a powerful logical library, which is several times more powerful than STL. When developing a project, find Qt or MFC on the interface, and use boost for complicated logic. You define the design in the middle.
There is nothing C ++ can do, but your application has made a lot of progress, mainly shortening the development cycle. Boost is the raw material library and wheel library. You don't need to worry about getting these things again.

As for what is in Boost, go to the website by yourself.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.