memry reusing是什麼意思?例如,我們已經創建並反對。記憶重用的真實含義是什麼?
struct A { };
A *a = new A;
void *p = operator new(sizeof(A),a); //Is it memory reusing?
void *p = realloc(sizeof(A),a); //Is it memory reusing?
我問這個問題,因爲3.8/6節中的例子讓我感到困惑。這個例子:
#include <cstdlib>
struct B {
virtual void f();
void mutate();
virtual ~B();
};
struct D1 : B { void f(); };
struct D2 : B { void f(); };
void B::mutate() {
new (this) D2; //1, reuses storage — ends the lifetime of *this
f(); // undefined behavior
... = this; // OK, this points to valid memory
}
也就是說,在//1
我們首先調用placement-new
其重用內存,以及緊接在該構造了一個新的對象。對?
爲什麼沒有定義行爲? – 2014-09-05 06:24:59
@ david.pfx:請參閱http://stackoverflow.com/questions/9117358/is-it-allowed-to-write-an-instance-of-derived-over-an-instance-of-base – 2014-09-07 12:03:49
放置表單的分配函數是* always * a no-op。 (庫的版本是,如果你替換庫的版本,標準會使你的整個程序不確定的行爲,參見'[new.delete.placement]') – 2014-09-07 12:06:00