Special case for new

Hey guys,
at the moment im programming some kind of my own memory smartpointer management system.
Im at this point where i dont know if i can build my design with c++.
At the moment the design looks like this: I got a Special allocator i want to place my objects in, with a call like this.
SmartPtr<MyObject> myPtr(new (allocator) MyObject);
Pretty simple, but i got some issues. My Smartpointer is designed to lookup the allocators "memory table" to find the right address to an object.
It works like this.
The Smartpointer stors an integer and calls some function of the allocator that looks like this
void* getAddress(int i);
so the allocator returns the address of the object i want to access.

Now back to the real problem.
If i create an object like i want to do in that first line of code, i need to rebuild the new operator so i can pass the size and a memory location and in return i get an integer that i can store in the smartpointer. The problem is, i cant return this integer becouse the object is created in the return address of te new call. So is it possible to overload new so it creates an object within the function and not afterwards? Or not at all?
Last edited on
I'm not sure if i correctly undersand you.
An allocator is normally something designed to allocate memory, here it seems you are talking of something else. What is it?
Who is in charge of calulate the integer? the "allocator"?
Who is in charge of allocating the memory? the user? the "allocator"?
It is possible to overload new operator but i'm not sure it's what you want here.
Thank you for your fast reply.
The allocator allocates and manages the memory. And also the allocator got some kind of table that stors an id for each memory block like a handle. I'd like to tell new it should take the memory from that allocator but insted of returning the memory address it should return that number that identifies the memory, so i can let the allocator defragment the momory without reasigning the correct memory adress over and over again.
The problem here is whatever i return from "new" c++ is try's to take it as an memory address and builds the object in it.
So i'd like to have the new syntax (for library's) but insted of allocating memory in it and build an object into that return address, it should just behave like this. I pass the memory address build an object at that address and return something, without an automatic try to build an object after returning from new.
Last edited on
I think you are talking about overloading the new operator (correct me if I'm wrong). Here, this might help you

http://www.cprogramming.com/tutorial/operator_new.html
The goal of new keyword is to do two things:
1) call operator new to allocate memory.
2) build the object in this memory
You can prevent 1) by using placement new: new (adress) Type(params), this will only construct the object
Or you can change the behaviour of 1) by overloading operator new.
But you can't prevent the construction of the object as far as I know using new keyword.
The only thing you can do is explicitely call operator new but as your allocator already do the job i think i's pointless.
Okay thanks for that answer. Could you tell me if it is possible to construct an object without operator new on the heap or in preallocated memory?
Something that does step 2. without step 1.?
it is possible to construct an object without operator new on the heap

NO.

however you can alocate using malloc but that's same.
I think RobertK might be wanting placement new. Which, yes, is very possible. I hope this is what you're looking for.

There are 3 different kinds of new:

1) "operator new" which allocates memory on the heap but does not do any object creation or ctor calls

2) "placement new" which places an object in already allocated memory. It calls ctors, but does not allocate any memory.

3) plain old "new" which does both #1 and #2


If you have preallocated memory and you just want to put an object there, then placement new is what you want.

Note there are caveats to this. One of them is that you must remember to manually call the destructor before memory is freed.

Here's more info:

http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.10
Last edited on
Thanks for your help guys, i kinda needed something like placement new but in a kinda different way.
I figured out that i cant overload new in that way i need it without editing how the compiler handels the object creation. So i kinda need to rethink my memory allocation design and restart.
Thank you for your help anyway.
Topic archived. No new replies allowed.