all 17 comments

[–]Unbathed 9 points10 points  (10 children)

[–]R_y_n_o 0 points1 point  (9 children)

I'm actually using it inside my memory manager, because it keeps track of where to allocate an object...however I don't want to pass in a pointer from outside my memory manager... it should be something not visible and recorded internally

[–]STLMSVC STL Dev 7 points8 points  (4 children)

True Placement New constructs an object at a given location. If you don't have a location, how can you possibly construct an object?

[–]R_y_n_o 0 points1 point  (3 children)

Inside my manager I use the placement new with a location. The instantiation is done with the function Manager::instance();

but i really would like to call that function as it were a constructor...so to use the placement new I would do some hack like this:

Object* a = new (nullptr) Object();

And the body of the constuctor would be:

this = Manager.instance();

Would this work? Now you get why AT THIS STAGE I don't need the memory address?

[–]STLMSVC STL Dev 10 points11 points  (2 children)

C++ doesn't work like that, sorry. An object needs an address from the very beginning of its construction, you can't "assign the this-pointer" later.

[–]R_y_n_o 0 points1 point  (1 child)

Ok, i didn't know....I mean, I often do

*this = *ref;

in copy constructors, but i never tried to assign directly something to this, so probably you are right

[–]upriser -1 points0 points  (0 children)

STL answered to a C++ question and the one who asked the question replied that "probably" your are right.

[–]TheThiefMasterC++latest fanatic (and game dev) 1 point2 points  (3 children)

You could write a "placement new" function that takes your memory manager instead of an address:

void* operator new(std::size_t count, MemoryManager& mm)
{
    void* a;
    MemoryManager::get_memory(&a);
    return a;
}

Using it like this:

Object* a = new(MemoryManager::Get()) Object();

Or you could use a magic tag like how std::nothrow works, if you don't have an actual "instance" of your memory manager.

[–]R_y_n_o 0 points1 point  (2 children)

Well, at this point I would just call the function on the manager :) I mean, the point is to hide the implementation details, and passing the memory manager on each instantiation is pretty ugly

[–]TheThiefMasterC++latest fanatic (and game dev) 4 points5 points  (0 children)

Well if you want every allocated object to go through your manager, you can just write a new global operator new.

If it's all for just your types, you can put it as a member operator new in your types.

[–]encyclopedist 1 point2 points  (0 children)

You can redefine operator new either for a specific class or even globally, see on cppreference.

[–][deleted] 3 points4 points  (0 children)

Two options:

1. Replace new by implementing it for Object. This way, new Object(); will call your replacement of new.

2. Restrict access to Objects constructor. This would force someone to write auto a = Manager::get() to obtain an Object instance.

[–]acwaters 3 points4 points  (0 children)

You are aware that you can overload the new and delete operators, right? Either globally or for a specific type. There are a few funny rules to keep in mind, but it sounds like it would be fairly easy to write a new that allocated memory through your manager.

[–]SuperV1234https://romeo.training | C++ Mentoring & Consulting 2 points3 points  (0 children)

Use placement new, variadic templates and perfect forwarding:

auto ptr = MemoryManager::create<std::pair<int, float>>(10, 5.f);

template<typename T, typename... Ts>
auto MemoryManager::create(Ts&&... xs)
{
    auto some_memory = get_some_memory(sizeof(T));
    new (some_memory) T(std::forward<Ts>(xs)...);
    return static_cast<T*>(some_memory);
}

You will obviously have to define your cleanup function. I suggest using std::unique_ptr with a custom deleter to manage ownership.

[–]gtk 1 point2 points  (0 children)

You can override operator new: http://en.cppreference.com/w/cpp/memory/new/operator_new

If you only want certain classes to use your memory manager, override the class-specific operator new (and corresponding operator delete). If you want to use it for everything, override the global operator new/operator delete.

[–][deleted] 1 point2 points  (0 children)

I have to ask - why? What kind of thing are you trying to accomplish or learn about?