One scenario would be when you want to implement your own behaviour (exception handling / error message to user / other disaster recovery ) while intitialising objects of your class.
Other scenario is when you want to pass arguments such as a memory pool to use while allocation memory for the object, or the maximum size.
These may not be the best uses but are what I can think of now.
Lets make this thread a good resource.. would be of great help (even to help me understand) if you guys could support each of your argument with a good simple example
The allocators implement different strategies for managing the memory pool in use. Some are specialized in managing BIG objects that rarely get delete'd and new'd others are better for lots of small objects.
So i'd say you'd always overload the new operator() when the default behaviour does not suit your needs. (And you verified this using a profiler to make sure that's where the problem lies.)
Yes and no. On your regular OS there is no way of knowing where the memory allocation will take place since you do not have control over the memory request function (sbrk(3) on linux).
But once you have a chunk of memory you can control where on that chunk you want your data to be setup in. See placement new for that.
Placement new is used when you have memory allocated into which you want to construct an object. For example, if you want to put an object into shared memory, you could do that with a placement new.
e.g, using placement new and overloaded operator new[ ] together you can call constructors with arguments when allocating an array. constructors with arguments by default is not supported by ::new operator when allocating an array.
As for the first part, the only advantage I believe is that you know that the string pointed to by p is stored in buf. That's really all. It just ensures the object is allocated at a particular spot. Not that useful IMO, but I don't really know much about it.
I have no idea about the second part. I'm really just guessing here, but it looks like you are making a pointer to a pointer to a parameterless function that returns and int and using new with it somehow? ?? I don't know off the top of my head, apologies.
Here is one more stuff that i am not able to understand..
void *mem = ::operator new(sizeof(A) * numOfArrElems);
A *a = static_cast<A*>(mem);
for(int i = 0; i < numOfArrElems; ++i) {
new (a[i]) A(args);
}
// Do your stuff....
// Call dtors on all elems and then delete the memory in the same fashion as was
// allocated
for(int i = 0; i < numOfArrElems; ++i) {
a[i].~A();
}
void *mem = ::operatornew(sizeof(A) * numOfArrElems);
A *a = static_cast<A*>(mem);
for(int i = 0; i < numOfArrElems; ++i) {
new (a[i]) A(args);
}
this is operator new+placement new.. and now you can allocate an array and call constructors simultaneously.
1 2 3
for(int i = 0; i < numOfArrElems; ++i) {
a[i].~A();
}
when using placement new, you need to call destructor explicitly. This is almost the only place in C++ you need to call destructors explicitly.
after this, you need to call operator delete, ::operatordelete (mem);