Introduction to STL configurator (Allocator)

Source: Internet
Author: User

If there is a class Arwen, then instantiate the class

Class Arwen {}

Arwen pwen = new Arwen; // the actual execution is performed in two steps. (1) call operator new to configure memory (2) Call Arwen: Arwen () to construct object content

Delete pwen; // There are two steps (1) Calling Arwen :~ Arwen () calls operator Delete to release the memory.

Operator new corresponds to malloc in C, and operator Delete corresponds to free in C.

 

Allocator in STL is equivalent to explicitly implementing the new and delete operators. There are four steps in total.

(1) Use allocate to configure the memory and deallocate to release the memory.

(2) construct is used to construct objects and destroy is used to analyze objects.

 

Of course, the actual implementation may not be as simple and complicated as described here.

Memory Allocation (allocate) and memory release (deallocate)

Memory Allocation

Template <class T>

T *Allocate(Size_t size)

{

T * TMP = (T *) (operator new (N * sizeof (t); // you can also use malloc in C. T * TMP = (T *) malloc (N * sizeof (t ));

Return TMP;

}

 

Release memory

Template <class T>

Void deallocate (T * P ){

Operator Delete (p); // If malloc is used earlier, free (P) is used );

}

 

Construct and destroy

That is, explicitly calling constructor and destructor. you may be confused. I usually finish using a new one, and the constructor will be automatically called. what can I do if I call it explicitly?
Here we need to use a new concept called Placement new. Three related but different concepts about new:

(1) New (2) operator new (3) Placement new

Among them, (1) New is the most commonly used one. It cannot be reloaded. When using it, operator new will always be automatically called to allocate memory, and then the constructor will be called to initialize the object.

(2) operator new is the same as malloc in C, but simply allocates memory. For example, int * IP = (int *) operator new (sizeof (INT ));

(3) Placement new is an overloaded version of operator new, but it does not allocate memory. It only returns the allocated memory address and initializes some variable values in the memory block.

So we use placement new to explicitly call the constructor. The usage is as follows:

Template <class TA, class TB>

Void construct (TA * P, const TB & Value ){

New (p) Ta (value); // This is the so-called placement new. it looks a little strange. there is more (p) than the general new, and this p is the memory address of the object.

}

 

The Destructor is simple.

Template <class T>

Void destroy (T * P ){

P-> ~ T (); // directly call the destructor.

}

 

You may wonder why you can directly call the destructor, but directly calling the constructor will be so troublesome. I don't understand this either. you can try to write code. it is assumed that the class Arwen is used.

Arwen * P = new Arwen;

P-> Arwen (); // An error occurred while calling the constructor directly.

P-> ~ Arwen (); // There is no problem in calling the destructor.

 

 

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.