Jump to content
  • Advertisement
Sign in to follow this  
Khatharr

Placement new

This topic is 2232 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

Note that while scalar (regular) placement new is useful, the array form is more or less useless. It won't reliably place the first object in the array at the address you give it, which pretty much defeats the purpose of using placement new in the first place.

 

Interesting.

 

What does it do then? Place it at the next highest address that satisfies the alignment requirements of the type? That sounds sane, if that is in fact what happens. It just places the additional constraint on you as a programmer that you have to ensure the address you give it is suitably aligned. I would have assumed the same for non-array placement new anyhow.. does it not balk if you hand it an unaligned address?

Share this post


Link to post
Share on other sites
Advertisement

What does it do then? Place it at the next highest address that satisfies the alignment requirements of the type? That sounds sane, if that is in fact what happens. It just places the additional constraint on you as a programmer that you have to ensure the address you give it is suitably aligned. I would have assumed the same for non-array placement new anyhow.. does it not balk if you hand it an unaligned address?

You're trying to think of useful behavior. No, what generally happens is that an implementation dependent amount of storage is used at the address you give the array form of placement new to store the number of elements in the array. You can't get rid of this extra data by giving placement new an aligned address. And there's no standard way to figure out how much extra space is needed.

Share this post


Link to post
Share on other sites

Okay, so I'm safe (theoretically) because I used a 1KB char buffer, but if extra storage is needed then how would you go about determining the total size needed to store the objects and extra data?

Share this post


Link to post
Share on other sites

You overwrite operator new[] (and operator delete[]) for the class and get the size in bytes including the hidden value as a parameter. As you know how large the class is from sizeof you can calculate the extra space.

Share this post


Link to post
Share on other sites

The problem with that is that placement new occurs after memory allocation rather than being the function that allocates the memory. By the time you've gotten the overhead information, you've already needed it. In any case, the better solution isn't to try to force the array form of placement new to work. Generally speaking, if you want to reach for the array placement new you probably want to grab std::uninitialized_fill() or std::uninitialized_fill_n() instead.

Share this post


Link to post
Share on other sites

In any case, the better solution isn't to try to force the array form of placement new to work.

 

The assignment calls for it. sad.png

 

Right now I have

struct chaff {
  char dross[20];
  int slag;
}; 

const size_t BUFFER_LENGTH = 1024;
char buffer[BUFFER_LENGTH];

int main() {
  const size_t CHAFF_COUNT = 2;
  _ASSERT((CHAFF_COUNT * sizeof(chaff)) <= BUFFER_LENGTH);

  ...

  return 0;
}

I guess I could just remove the bounds check and stop being pedantic. unsure.png

 

Actually, I think I'll just leave a comment by the assertion. This is way too much effort for this kind of assignment.

Share this post


Link to post
Share on other sites

I once used placement new in a serialization scheme.

 

I used it with special "do nothing" constructors to fix up the virtual function table pointers on object images loaded from disk.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!