• Advertisement
Sign in to follow this  

new[] is flawed?

This topic is 1560 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Why don't they store the housekeeping data preceding the allocation then, making sure the returned pointer is aligned? Operator delete[] could find the housekeeping information based on the pointer passed to it, via the magic of subtraction.

 

Seems a no-brainer to me... (maybe the implementation was implemented before alignment became a major issue though, and it is retained for backwards compatibility).

This is how I always thought it worked. AFAIK, the housekeeping data is just the length of the array, so delete[] knows how many objects to call the destructor on. I just ran a test in GCC and this is indeed how it works (count stored at ReturnedAddress - 4)

 

I tried placement new[] as well, and the pointer returned is the same as the pointer passed in, and I can't find the count stored in memory anywhere shortly before or after the data. Also, calling delete[] on it crashes. I'm pretty sure destructors have to be manually called on placement new'd things, since there's nothing marking that the delete shouldn't also release the memory either.

Edited by DekuTree64

Share this post


Link to post
Share on other sites
Advertisement

Yes, if you call placement new you need to call the destructor manually, via obj->~ObjectType() (since placement new didn't allocate the memory, and doesn't know where it came from, it can come from anywhere, a global pool, on the stack, etc.).

 

You never call delete on a placement new constructed object, since it wasn't created via operator new.

Edited by Paradigm Shifter

Share this post


Link to post
Share on other sites

Hmm I think I remember what you're talking about, somethinga bout knowing how many objects to call the destructor for. The strange thing was that it wouldn't do it on some pointers, but would on others. I just wanted to make sure there wasn't a way to solve this issue, because I really like having arrays of objects. It's so nice for cache coherency!

The way to solve this would presumably be to overload operator new[], such that it returns properly aligned data. Possibly writing it in terms of regular operator new. Don't forget top overload operator delete[] too if you do that.

Share this post


Link to post
Share on other sites

To answer the original question: yes, new[] is a bit flawed in that regard.

 

As soon as you allocate an array of non-PODs with new[], most compilers will add an extra 4 bytes to store the number of elements in the array. This is needed in order to call the destructors as soon as delete[] is invoked. I've written about the mechanism in more detail on my blog. I've yet to see a compiler which does it differently, do note though that this is not required per the standard - the compiler can store this book-keeping data where and how he sees fit. 

 

Note that if you properly declare the corresponding non-POD class type as being aligned (e.g. using __declspec(align(n)) on MSVC), you can use your own overload of operator new, and will get a properly aligned pointer. However, if you need to allocate an array of aligned non-PODs whose declaration you cannot change (e.g. a class type in a 3rd-party library), you're basically out of luck. Because of that, several engines I know of do not use new, delete, new[] or delete[], but rather their own implementations.

 

Hope that helps!

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites


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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement