Placement new

Started by
16 comments, last by vreality 10 years, 7 months ago

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?

throw table_exception("(? ???)? ? ???");

Advertisement

Shit. I need to check my homework.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

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.

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?

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

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.

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.

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.

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

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.

This topic is closed to new replies.

Advertisement