Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storage allocator - what is it?

Tags:

c++

memory

I know of storage classes in both C and C++ (static, extern, auto, register, C++ also adds mutable and some compiler-specific ones) but I can't figure out what a storage allocator is. I don't think it's referred to memory allocators implementable on STL, what is it in simple terms?

like image 846
Johnny Pauling Avatar asked Feb 27 '13 15:02

Johnny Pauling


2 Answers

It's whatever is behind operator new and operator delete (not to be confused with the new operator and the delete operator). operator new allocates memory from the free store, and operator delete releases memory previously allocated by operator new for possible reuse. When code does foo *ptr = new foo (new operator), the compiler generates code that calls operator new to get the right number of bytes of storage, then calls the constructor for foo. When code does delete ptr (delete operator) the compiler calls the destructor for foo, then calls operator delete to release the memory.

Note that this is how the term is used in the C++03 standard. In the C++11 standard it is also used to refer to standard allocators.

like image 183
Pete Becker Avatar answered Sep 21 '22 05:09

Pete Becker


In the C++ standard, that term is used to refer to the allocator class used by STL-style containers - either std::allocator, or a user-defined custom allocator that meets the requirements given by C++11 17.6.3.5.

However, it's not a formally defined term, and also appears once referring to the implementation of the free store - that is, the dynamic storage allocated by new.

[NOTE: I'm referring to the current (2011) language specification. As noted in the comments, historical versions of the specification apparently only used the term (informally) to refer to the free store]

like image 26
Mike Seymour Avatar answered Sep 20 '22 05:09

Mike Seymour