C ++ Effective use of the new operator

When creating an instance of a class with a new one. Instead of deleting the memory, what benefits will we get based on reusing objects?

What is the process of the new? Does a context switch occur? New memory is allocated, who performs the allocation? OS?

+3
c ++ memory
source share
3 answers

You asked a few questions here ...

Instead of deleting the memory, what benefits will we get based on reusing objects?

It totally depends on your application. Even assuming I know what the application is, you left one more detail unclear - what is the reuse strategy? But even knowing that it is very difficult to predict or answer in general. Try some things and measure them.

As a rule, I like to minimize the most free distribution. However, this is premature optimization. This will only matter for thousands of calls.

What is the process of the new?

It depends entirely on the implementation. But the general strategy that distributors use is to have a free list, that is, a list of blocks that were freed in the process. When a free list is empty or contains insufficiently continuous free space, it should request a kernel for memory, which it can only issue in blocks with a constant page size. (4096 on x86.) The distributor must also decide when to cut, lay, or combine blocks. Multithreading can also put pressure on distributors, as they must synchronize their free lists.

As a rule, this is a rather expensive operation. Perhaps not so much in relation to what else you are doing. But it is not cheap.

Does a context switch occur?
Quite possible. It is also possible that this will not happen. Your OS can freely switch to context at any time when it receives an interrupt or syscall, so ... This can happen many times; I do not see any special relationship between this and your distributor.

New memory is allocated, who performs the allocation? OS?
This can come from a free list, and in this case the system call is not involved, therefore, no help from the OS. But this can come from the OS if the free list cannot satisfy the request. In addition, even if this comes from a free list, your kernel could upload this data so that you can get a page error on access, and the kernel allocator will work. Therefore, I think it will be a mixed bag. Of course, you can have an appropriate implementation that does all kinds of crazy things.
+5
source share
  • new allocates memory for the class on the heap and calls the constructor.
  • context switches do not have to be executed.
  • C ++ - the runtime allocates memory on its freestyle using any mechanism that it considers necessary.

Typically, a C ++ runtime allocates large blocks of memory using OS memory management functions, and then subdivides them into its own heap implementation. The microsoft C ++ runtime environment mainly uses the Win32 heap functions, which are implemented in usermode, and share the OS memory allocated using virtual memory apis. Thus, there are no context switches until his current virtual memory allocation is required, and he needs to go to the OS to allocate more.

There is a theoretical problem with memory allocation that there is no upper bound on how long a heap bypass can take to find a free block. In practice, heap allocations are usually fast.

With the exception of streaming applications. Since most C ++ runtimes use a single heap between multiple threads, heap access must be serialized. This can seriously degrade the performance of certain classes of applications that rely on multiple threads, capable of new and deleting many objects.

+2
source share

If you are a new or delete address, it is marked as busy or unassigned. Implementations do not speak all the time with the kernel. Larger cartridges of memory are reserved and divided into smaller cartridges in user space inside your application.

Since new and delete are repetitive (or thread safe depending on the implementation), a context switch may occur, but your implementation will be thread safe anyway using the standard new and delete .

In C ++, you can overwrite the new and delete operator, for example. for memory management:

 #include <cstdlib> //declarations of malloc and free #include <new> #include <iostream> using namespace std; class C { public: C(); void* operator new (size_t size); //implicitly declared as a static member function void operator delete (void *p); //implicitly declared as a static member function }; void* C::operator new (size_t size) throw (const char *){ void * p = malloc(size); if (p == 0) throw "allocation failure"; //instead of std::bad_alloc return p; } void C::operator delete (void *p){ C* pc = static_cast<C*>(p); free(p); } int main() { C *p = new C; // calls C::new delete p; // calls C::delete } 
+1
source share

All Articles