将堆栈内存的地址传递给放置新运算符 [英] Passing address of stack memory to placement new operator

查看:54
本文介绍了将堆栈内存的地址传递给放置新运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码来自第11节中的c ++ faq:


void someCode()

{

char memory [sizeof (Fred)];

void * p =记忆;

Fred * f = new(p)Fred();

f-> ;〜佛瑞德(); //显式调用析构函数以获取已放置的

对象

}


这里我们将堆栈内存的地址传递给new。

New用于在堆上分配内存。然后我们如何将地址

堆栈内存传递给新运算符。令人困惑。


问候,

Mangesh。

This code is from c++ faq in section 11 :

void someCode()
{
char memory[sizeof(Fred)];
void* p = memory;
Fred* f = new(p) Fred();
f->~Fred(); // Explicitly call the destructor for the placed
object
}

Here we r passing address of stack memory to new .
New is used to allocate memory on heap . Then how can we pass address
of
stack memory to new operator . It is confusing .

Regards ,
Mangesh .

推荐答案

* mangesh:
* mangesh:
此代码来自第11节中的c ++ faq:

void someCode()
{char char memory [sizeof( Fred)];
void * p = memory;
Fred * f = new(p)Fred();


应该是


Fred * f = :: new(p)Fred();


f-> ~Fred(); //显式调用已放置
对象的析构函数


这里我们将堆栈内存的地址传递给new。
New用于在堆上分配内存。那我们怎么能把堆栈内存的地址传递给新的运营商呢。令人困惑。
This code is from c++ faq in section 11 :

void someCode()
{
char memory[sizeof(Fred)];
void* p = memory;
Fred* f = new(p) Fred();
Should be

Fred* f = ::new(p) Fred();

f->~Fred(); // Explicitly call the destructor for the placed
object
}

Here we r passing address of stack memory to new .
New is used to allocate memory on heap . Then how can we pass address
of
stack memory to new operator . It is confusing .




基本的贴牌新操作符(它们有无穷大,但

来自< new> )在指定的内存区域构造一个对象。


你不应该使用它,因为它是一种颠覆

的低级机制普通的语言规则,因此充满了危险,甚至比演员表更多,例如,仅作为一个例子,这里记忆可能不会是b $ b b正确对齐''Fred''类型的对象,特别是:


*新手应该/永远/使用新的基本位置。


使用

标准库类(例如std :: vector)更好地表达(基本)placement new的大多数用法,它们为你做了危险的

的东西,以安全的方式,让你甚至看不到它。


-

答:因为它弄乱了人们的秩序通常阅读文字。

问:为什么这么糟糕?

A:热门发布。

问:usenet和e-最令人烦恼的是什么?邮件?



The basic placement new operator (there are an infinity of them, but the
basic one from <new>) constructs an object in a specified region of memory.

You shouldn''t use it because it''s a low-level mechanism to subvert the
ordinary language rules and as such is fraught with dangers, even more
than with casts, e.g., as just one example, here that ''memory'' may not
be properly aligned for an object of type ''Fred'', and in particular:

* A novice should /never/ use the basic placement new.

Most uses of (the basic) placement new are better expressed using
standard library classes such as std::vector, which do the dangerous
stuff for you, in a safe way, so that you don''t even see it.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


mangesh写道:
mangesh wrote:
void someCode()
{char memory [sizeof(Fred)];
void * p =记忆;
Fred * f = new(p)Fred();
f-> ~Fred(); //显式调用已放置
对象的析构函数


这里我们将堆栈内存的地址传递给new。


我发现令人费解的是,您可以花时间在标点符号周围放置奇怪的空格

,但单词中的三个字母是当你要求别人帮助你的时候,以可读性的名义付出太多的努力。


无论如何,是的,你在堆栈分配的数组上做了一个新的放置。

新用于在堆上分配内存。


不完全 - 它在免费商店分配内存,这可能或者可能不会以堆的形式实现
。它也可能被

a自定义分配器覆盖,顺便说一下,它可能会使用

的新位置。

然后我们如何传递地址<
堆栈内存给新运营商。这很令人困惑。
void someCode()
{
char memory[sizeof(Fred)];
void* p = memory;
Fred* f = new(p) Fred();
f->~Fred(); // Explicitly call the destructor for the placed
object
}

Here we r passing address of stack memory to new .
I find it baffling that you can take the time to place weird spaces
around your punctuation, but the three letters in the word "are" are
too much effort in the name of readability when you''re asking others to
help you.

Anyway, yes, you''re doing a placement-new on a stack-allocated array.
New is used to allocate memory on heap .
Not quite -- it allocates memory on the free store, which may or may
not be implemented in terms of the heap. It may also be overridden by
a custom allocator, which incidentally would likely make use of
placement new.
Then how can we pass address
of
stack memory to new operator . It is confusing .




因为一个新的位置没有分配内存,所以它使用了你为它分配的
的内存。这就是重点。它会跳过

分配步骤并直接进入构建阶段。它并不关心

指针所指向的内存部分,只要它可以写成

to。


想象一下,例如,为小对象实现自定义分配器

基于固定大小的竞技场分配为单个块。那个块

可能是分配器类的成员数据,因此可能是
堆栈分配。当然,如果allocator类动态地实例化了
,那么它的成员数据将存在于免费商店中。由于

可能就是这种情况,如果编译器

无法处理堆栈分配内存中的新位置,则会出现真正的问题。


Luke



Because a placement new doesn''t allocate memory, it uses memory that
you''ve allocated for it. That''s the entire point. It skips the
allocation step and goes straight to construction. It doesn''t care
what part of memory the pointer points to, as long as it can be written
to.

Imagine, for example, implementing a custom allocator for small objects
based on a fixed-size arena allocated as a single chunk. That chunk
might be member data of an allocator class, and could therefore be
stack-allocated. Of course, if the allocator class was instantiated
dynamically, then its member data would live on the free store. Since
either could be the case, it would pose a real problem if the compiler
couldn''t handle doing a placement new into stack-allocated memory.

Luke


您好,

感谢您的回复。

现在在给定的情况下是声明 f-> ~Fred(); "确实需要。

由于内存在堆栈上,它将在退出

功能时自动删除。


问候

Mangesh。


Luke Meyers写道:
Hi ,
thanks for reply .
Now in given case is statement " f->~Fred() ; " really needed .
Since memory is on stack it will be automaticaly deleted on exiting
function .

Regards
Mangesh .

Luke Meyers wrote:
mangesh写道:
mangesh wrote:
void someCode()
{
char memory [sizeof(Fred)];
void * p = memory;
Fred * f = new(p)Fred();
f-> ~Fred( ); //明确调用已放置
对象的析构函数


这里我们将堆栈内存的地址传递给新的。
void someCode()
{
char memory[sizeof(Fred)];
void* p = memory;
Fred* f = new(p) Fred();
f->~Fred(); // Explicitly call the destructor for the placed
object
}

Here we r passing address of stack memory to new .



我觉得令人费解的是,你可以花点时间在你的标点符号周围放置奇怪的空格,但是是字样中的三个字母。当你要求别人帮助你的时候,以可读性的名义付出了太大的努力。

无论如何,是的,你正在做一个新的安置堆栈分配的数组。



I find it baffling that you can take the time to place weird spaces
around your punctuation, but the three letters in the word "are" are
too much effort in the name of readability when you''re asking others to
help you.

Anyway, yes, you''re doing a placement-new on a stack-allocated array.

新用于在堆上分配内存。
New is used to allocate memory on heap .



不完全 - 它在免费商店分配内存,可能或可能不会在堆方面实现。它也可能被自定义分配器覆盖,这可能会使用新的
位置。



Not quite -- it allocates memory on the free store, which may or may
not be implemented in terms of the heap. It may also be overridden by
a custom allocator, which incidentally would likely make use of
placement new.

那我们如何传递地址
堆栈内存给新运营商。这很令人困惑。
Then how can we pass address
of
stack memory to new operator . It is confusing .



因为一个新的位置没有分配内存,所以它使用了你为它分配的内存。这就是重点。它跳过了
分配步骤并直接进行构建。它并不关心
指针指向的内存部分,只要它可以写成

想象一下,例如,实现一个自定义分配器基于固定大小的竞技场分配为单个块的小对象。那个块可能是分配器类的成员数据,因此可能是堆栈分配的。当然,如果分配器类是动态实例化的,那么它的成员数据将存在于免费商店中。由于
可能是这种情况,如果编译器无法处理堆栈分配内存中的新位置,则会出现真正的问题。

Luke



Because a placement new doesn''t allocate memory, it uses memory that
you''ve allocated for it. That''s the entire point. It skips the
allocation step and goes straight to construction. It doesn''t care
what part of memory the pointer points to, as long as it can be written
to.

Imagine, for example, implementing a custom allocator for small objects
based on a fixed-size arena allocated as a single chunk. That chunk
might be member data of an allocator class, and could therefore be
stack-allocated. Of course, if the allocator class was instantiated
dynamically, then its member data would live on the free store. Since
either could be the case, it would pose a real problem if the compiler
couldn''t handle doing a placement new into stack-allocated memory.

Luke






这篇关于将堆栈内存的地址传递给放置新运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆