new[] is flawed?

Started by
37 comments, last by tivolo 10 years, 6 months ago

Sorry I'm a little late to replying, I didn't think this thread would blow up like it did.

I do have a question for tivolo, we initially thought about not using new delete new[] or delete[] but we didn't want to forsake our constructors and the like. Is there a good way around this, or do you just use Initialize() methods on all of your objects?

Perception is when one imagination clashes with another
Advertisement

The canonical fix is to write a macro that invokes placement new on memory returned from your custom allocator.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Will placement new still have house keeping information that it needs to store? Would the pointer returned to the client be at the same position as the placement?

Perception is when one imagination clashes with another

The standard guarantees that the pointer returned by placement new (both scalar and array variants) is equal to, and that the objects are constructed at, the pointer you pass to them. If you need any information about the number of objects to destroy later, you need to keep that information yourself.


The standard guarantees that the pointer returned by placement new (both scalar and array variants) is equal to, and that the objects are constructed at, the pointer you pass to them.

No, it doesn't. Not for the array placement new. 5.3.4 paragraph 12 of the standard specifically states that the pointer returned by array placement new may be offset, and in practice most compilers will for complex types.

Section 18.6.1.3 lists the special forms of placement new where you pass the pointer to the memory location where the objects are constructed.

Yes, they exist. I never said that they didn't. But implementations are still permitted to add array allocation overhead to array placement new as described in 5.3.4. And even if they weren't allowed to do it, it doesn't change the fact that most implementations do anyways.

Actually, the way I see it now is that section 18.6.1.3 lists the behavior of the function named operator new that the operator new calls to acquire memory before calling the constructors and whatever other book keeping it will do. Different functions to acquire the memory, but it's still the same operator new for initializing the array; book keeping included.

Sorry I'm a little late to replying, I didn't think this thread would blow up like it did.

I do have a question for tivolo, we initially thought about not using new delete new[] or delete[] but we didn't want to forsake our constructors and the like. Is there a good way around this, or do you just use Initialize() methods on all of your objects?

You don't have to forsake constructors and the likes.

But you have to do the things the compiler normally does for you yourself. That means that you have to use placement new on memory returned by your custom allocator, and also have to make sure the constructor/destructors are correctly invoked for arrays. I use regular placement new both for single instances as well as arrays, and don't rely on array placement new.

You can find really detailed info on my blog, search for "memory system", it's a five-part series.

This topic is closed to new replies.

Advertisement