Detailed explanation of memory allocation problems caused by new and delete in C + +

Source: Internet
Author: User

One common problem with C + + in embedded systems is memory allocation, which is out of control of the new and delete operators.

Ironically, the root cause of the problem is that C + + management of memory is very easy and secure. Specifically, when an object is eliminated, its destructor can safely release the allocated memory. This is a good thing, of course, but the simplicity of this use allows programmers to overuse new and delete, without paying attention to the causal relationship in the embedded C + + environment. Moreover, in embedded systems, due to the limitation of memory, the frequent dynamic allocation of variable size memory can cause great problems and the risk of heap breakage.

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 the memory allocations in C + +. You need to replace the system's memory allocations with a global new and delete, and overload new and delete for one class.

A common way to prevent heap fragmentation is to assign different types of objects from different fixed sized memory holds. This control is provided for each class overload new and delete.

Overload the global new and delete operators

The new and delete operators can be overloaded easily, as follows:

void * operator new(size_t size)
{
 void *p = malloc(size);
 return (p);
}
void operator delete(void *p);
{
 free(p);
}

This code can replace the default operator to satisfy the memory allocation request. For the purpose of interpreting C + +, we can also invoke malloc () and free () directly.

You can also overload the new and delete operators of a single class. This is the flexibility with which you can 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 all memory allocations for TestClass objects. Further, any class that inherits from TestClass also takes this approach, unless it overloads the new and delete operators themselves. By overloading the new and delete operators, you are free to use different allocation policies to assign different classes of objects from different memory pools.

Overloading new[] and delete[for a single class must be careful with the allocation of object arrays. You may want to invoke the new and delete operators that you have overloaded, but that is not the case. Memory requests are directed to the global new[] and delete[] operators, which come from the system heap.

C + + Allocates the memory allocation of an object array as a separate operation, but differs from the memory allocation of a single object. To change this way, you also need to overload 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;
}

But note: For most C + + implementations, the number parameter in the new[] operator is the size of the array plus some bytes of the extra storage object number. It is important to consider this in your memory allocation mechanism. You should try to avoid allocating an array of objects so that your memory allocation strategy is simple.

Related Article

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.