Tag: 放置 新

数组放置 – 新需要缓冲区中的未指定的开销?

5.3.4 C ++ 11月2日草案的[expr.new]给出了例子: new(2,f) T[5]导致operator new[](sizeof(T)*5+y,2,f)的调用。 这里,x和y是表示数组分配开销的非负非指定值; 新expression式的结果将会被operator new[]返回的值所抵消。 这个开销可以应用于所有数组的新expression式 ,包括那些引用库函数operator new[](std::size_t, void*)和其他位置分配函数的expression式。 从一个新的调用到另一个调用的开销可能不同。 – 例子 ] 现在拿下面的例子代码: void* buffer = malloc(sizeof(std::string) * 10); std::string* p = ::new (buffer) std::string[10]; 根据上面的引用,第二行new (buffer) std::string[10]将在内部调用operator new[](sizeof(std::string) * 10 + y, buffer) std::string对象)。 问题是,如果y > 0 ,预分配的缓冲区将太小! 那么如何知道在使用array-new时需要预先分配多less内存呢? void* buffer = malloc(sizeof(std::string) * 10 + how_much_additional_space); std::string* […]