Control the memory allocation of C ++

Source: Internet
Author: User
Author: Source: Editor of Forum Responsibility: A common problem of using C ++ in the embedded system of Fangzhou is memory allocation, which means the new and delete operators are out of control.

It is ironic that the root cause of the problem is that C ++ manages the memory very easily and securely. Specifically, when an object is eliminated, its destructor can safely release the allocated memory.

This is of course a good thing, but the simplicity of this use makesProgramThey use new and delete excessively, but do not pay attention to the causal relationship in the embedded C ++ environment. In addition, in embedded systems, frequent and Dynamic Allocation of memory of an indefinite size due to memory restrictions can cause great problems and the risk of heap fragmentation.

As a piece of advice, 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 the memory allocation in C ++. You need to replace the system memory allocation operator with a global new and delete, and reload the new and delete operations of a class and a class.

A common method to prevent heap fragmentation is to allocate different types of objects from memory holding of different fixed sizes. This control is provided for each class to overload new and delete.

Reload global new and delete Operators

The new and delete operators can be easily reloaded, as shown below:

Void * operator new (size_t size)
{
Void * P = malloc (size );
Return (P );
}
Void operator Delete (void * P );
{
Free (P );
}

This sectionCodeThe default operator can be used to meet memory allocation requests. For the purpose of interpreting C ++, we can also directly call malloc () and free ().

You can also overload the new and delete operators of a single class. This allows you to flexibly control the memory allocation of objects.

Class testclass {
Public:
Void * operator new (size_t size );
Void operator Delete (void * P );
//... Other Members here...
};

Void * testclass: Operator new (size_t size)
{
Void * P = malloc (size); // replace this with alternative Allocator
Return (P );
}
Void testclass: Operator Delete (void * P)
{
Free (p); // replace this with alternative de-Allocator
}

This code is used for memory allocation of all testclass objects. Furthermore, any class inherited from testclass also adopts this method, unless it also reloads the new and delete operators. By reloading the new and delete operators, You can freely use different allocation policies to allocate different class objects from different memory pools.

Reload new [] and delete [] for a single class

You must be careful with the allocation of object arrays. You may want to call the new and delete operators that have been reloaded by you, but this is not the case. Memory requests are directed to the global new [] and delete [] operators, and the memory comes from the System Heap.

C ++ uses the memory allocation of the object array as a separate operation, which is different from the memory allocation of a single object. To change this method, you also need to reload the new [] and delete [] operators.

Class testclass {
Public:
Void * operator new [] (size_t size );
Void operator Delete [] (void * P );
//... Other Members here ..
};
Void * testclass: Operator new [] (size_t size)
{
Void * P = malloc (size );
Return (P );
}
Void testclass: Operator Delete [] (void * P)
{
Free (P );
}
Int main (void)
{
Testclass * P = new testclass [10];

//... Etc...

Delete [] P;
}

However, note: For most implementations of C ++, the number parameter in the new [] operator is the size of the array plus some additional bytes that store the number of objects. This is an important consideration in your memory allocation mechanism. You should try to avoid allocating an array of objects so that your memory allocation policy is simple.

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.