One more Interview question on new operator Overload

when you will provide overloaded new operator. give example
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.
Last edited on
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.)
Will we be able to determine the address for allocation of memory??


say if I wan C++ to alocate address from 0x098A2 to 0x098A9 to a pointer, will be able to do that?
karthick88, Where are you getting the interview questions from ? Is there a book ?

PS: Your settings are currently not allowing PMs.
nope.. just googling...
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.
Well, I did google through for placement new but couldnt really get hold on it... can any of guys explain what it is and how it can be used?
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.
ok.. lets take an example

char *buf = new char[1000]; //pre-allocated buffer
string *p = new (buf) string("hi"); //placement new
string *q = new string("hi"); //ordinary heap allocation

So how what is happening her with p...??? yeah it puts in the buffer.. but whats the advantage here??



Also, I came across this snippet in msdn.. I have no clue as whats happening..

int (**p) () = new (int (*[7]) ());
delete *p;
Last edited on
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();
}
1
2
3
4
5
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);
}

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,
::operator delete (mem);
@everid .. Can you explain me a bit more? l

you can allocate an array and call constructors simultaneously


When we allocate array constructors are called simultaneously even with the normal new operator rite??
Topic archived. No new replies allowed.