|
|
|
|
allocator class
? I know it's for types that haven't a default value but Why can't we just use new
instead of that allocator class
? Both new
and malloc
get an amount of memory from the main memory. allocate(int n)
the code T* p = (T*)malloc(sizeof(T)*n);
I think means: Get an amount of memory of size T
, multiplied by n
, and convert it to a "pointer to T" then assign it to p
. Right?construct(T* p, const T& v);
function?p = new (p) T(v);
I know it's for types that haven't a default value but Why can't we just use new instead of that allocator class? |
new
allocates memory and calls the constructor. For a container it is desirable to allocate the memory for several objects beforehand. Later, when the user actually want to create the object, only the constructor is called on the precreated memory.
Sometimes you have special memory requirements, where you want to keep something in a special place, or you want to do something like keep track of how memory is used, etc. Allocators allow you to hook the memory management for that particular class |
malloc
and new
hook memory from heap. new
, too, gives us some bytes of that memory for us to use it the way we like. p = new (p) T(v);
T
with the value v
in p
. I meant you to explain the syntax or the code.new
rather than malloc
? Doesn't new
have any advantages? I think of memory as a store of raw places. What special memory requirements? |
new
or malloc
won't suffice.what other types don't have default values? |
class A { A(int) {} };
. Try declaring an array of those. But you can create a vector of those, because of allocators.p = new (p) T(v);
T
, initialized with the value v
, at the location referred to by p
"Doesn't new have any advantages? |
std::malloc
.
The default memory allocation in C++ is allocation of memory from the heap via operator new ... Conceivable non-default memory management schemes include use of pre-allocated memory pools, thread-specific memory, memory shared among processes, garbage-collected memory, persistent stores, etc. https://www.angelikalanger.com/Articles/C++Report/Allocators/Allocators.html |
Maybe you want to allocate from a memory pool - i.e., out of a 1GiB pre-allocated block, like many games, or dynamically from the stack. Maybe you must place a object at exactly a particular memory location (i.e., overlaying a MMIO register in an embedded processor). Maybe you're allocating memory from external storage, for lack of space in main memory. |
For each of those cases, plain new or malloc won't suffice. |
malloc
can give us, in this case (the program), while new
can't?I don't know what else to say |
new
can get/hook some part of "a memory" and also it can initialize it, but malloc
can only get/hook that some part of memory and needs new
for initializing. I'm not sure whether it's correct.It calls the constructor |
T
?The constructor of the type T? |
So here both result in the same, so I can use new instead of malloc!! |
new
instead of malloc
, as long as you were careful to not cause any inappropriate side effects (calling constructors, e.g.). n
bytes, both
|
|
new
can throw an exception (if you don't also pass std::nothrow
).T
at p1
or p2
using the placement-new syntax, assuming that sizeof (T) <= n
.In addition to allocating memory, new T would construct an object of type T in the allocated storage. |
new
is a typed way of allocating memory while malloc
is non-typed. new
also gets memory based on its type and malloc
based on the number of bytes. So malloc
is really raw, simple and free compared to new
. class My_allocator
is useless and I could use new
perfectly and easily instead of that and the result would have no difference with using malloc
. So I think by this code sniped, the author has just wanted us to be familiar with malloc
.make_unique
and use it instead of the both in the code.reserve(int newalloc)
function in line 5 above, we have: T* p = alloc.allocate(newalloc);
. Here the memory is hooked. In line 6, there is: alloc.construct(&p[i], elem[i]);
. And in the alloc.construct
we have new
that "again hooks memory" of that size (T
) which was hooked by malloc
beforehand!! Couldn't it use *p = v;
? construction: initialise raw storage so that there is an object (create an object) |
new
by that, well, we will have an extra memory hooking by new
, here.assignment: modify the state of an already existing object (the object must have been constructed earlier). |
T* p = alloc.allocate(newalloc);
creates a pointer, p, to an object of type T. Consider we have declared an int or double vector, so p will be a pointer to an int/double.new
.
|
|