1

I've known what the memory fragmentation is and why malloc or new in C/C++ can cause this issue.

Here is a link about memory fragmentation: What is memory fragmentation?

My question is if std::allocator can solve this issue effectively?

Saying that I have got a piece of memory with std::allocator, if I new and destruct frequently on this std::allocator, will I have the same issue: memory fragmentation? If so, is there some other methods to solve this issue in C++ level?

Yves
  • 8,474
  • 7
  • 59
  • 114
  • 1
    The people who write compilers' C++ libraries are pretty smart and know what they're doing. A lot of sweat and tears went into optimizing these low level details for most common and popular use cases. Unless an application's memory allocation and usage patterns are somehow strangely unique, bizarre, and way out there in the left field, it is highly unlikely that the application can outsmart and do a better job with its own allocator than with the standard C++ library's one. – Sam Varshavchik Dec 05 '19 at 04:26
  • @SamVarshavchik so do you mean that using `std::allocator` could avoid memory fragmentation generally speaking? – Yves Dec 05 '19 at 05:00
  • @SamVarshavchik Well, I just realized that `std::allocator` has no business with memory fragmentation... `std::allocator` just allows us to have more control comparing with `new` and `new[]`. Ofc, as `std::allocator` specifies the memory zone, it might improve the performance comparing with raw `new`. – Yves Dec 05 '19 at 07:38

0 Answers0