'placement'new,以及在构造函数中的分配。 [英] 'placement' new, and allocation in the constructor.

查看:74
本文介绍了'placement'new,以及在构造函数中的分配。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样的课Foo:


class Foo

{

public:

Foo()

{

mData = new char [time()%1000];

}

~Foo()

{

delete [] mData;

}

private:

char * mData;

};


现在如果我使用''placement''创建一个Foo对象,就像这样:

void * raw = malloc(sizeof(Foo()));

Foo * foo = new(raw)Foo();


foo.mData不会从我的内存池中获取,对吗?没有

方式知道Foo()将分配多少内存。但它*将* b $ b很好的mData内存与我的foo对象在同一个池中。


我忽略的任何提示或技巧,这将允许我这样做

(无法访问Foo类)?


谢谢,

~S

解决方案

Shea Martin写道:

如果我有这样的课Foo:

类Foo
{
公开:
Foo()
{
mData = new char [time()%1000];
}
~Foo ()
{
删除[] mData;
}
私人:
char * mData;
};

现在如果我使用''placement''创建一个Foo对象,就像这样:
void * raw = malloc(sizeof(Foo()));
Foo * foo = new(raw)Foo() ;

foo.mData不会从我的内存池中获取,对吗?


哪一个是你的内存池?基本上''malloc''和''new''

(或''new []'')在所谓的免费商店中执行分配。对于

,我们都知道,它是同一个内存池。对于他们两个。

没有方法知道Foo()将分配多少内存。但是*将* mData内存放在与我的foo对象相同的池中是很好的。


是吗?真?为什么?

我忽略的任何提示或技巧,这将允许我这样做(无法访问Foo类)?



你_could_提供你自己的''malloc''和''new []''。如果您确定知道

''Foo''使用" new []",那么您只需要重载函数

称为''::运算符新的[]''并在那里做你需要的。


V

-

请从我的地址删除资金通过邮件回复


Victor Bazarov写道:

哪一个是你的内存池?基本上''malloc''和''new''
(或''new []'')在所谓的免费商店中执行分配。对于我们所知道的,它是同一个内存池。对于他们两个。
真的吗?当我说出我的记忆

游泳池时,你不知道我在说什么?虽然你是对的,我并没有明确说明''我的记忆

pool''是一个记忆,除了免费商店以外的地方,但是

大多数人可能会自己连接点。恕我直言,你可能也会这样做,但是因为你是一个精英主义者,你选择了讽刺。

没有
方式知道Foo()将分配多少内存。但是*将* mData内存放在与我的foo对象相同的池中会很好*
会吗?真?为什么?



当使用有限的内存而没有虚拟内存时,它会有助于获得内存预算。特别是当有不同的

小组在项目的不同部分工作时。如果我分配内存,

我只想从我的池中拿走,如果另一组中的某个人为某些东西分配了b / b,我希望他们的对象只使用来自<他们的游泳池是
。但我敢打赌你可能已经猜到了这样的东西,但是选择了这个聪明的屁股。

任何提示或者我正在忽略的技巧,这将允许我这样做(没有访问Foo类)?
你_could_提供你自己的''malloc''和''new []''。如果您确定知道''Foo''使用" new []",那么您只需要重载名为'':: operator new []''的函数并执行操作你需要那里。



谢谢。一个值得用于存储它的字节的建议。覆盖默认运算符new和malloc的

问题不会强迫应用程序使用我的new / malloc。
。当3组都拥有自己的内存预算时,这并不是真正的帮助。虽然,它确实有帮助,因为我可以执行一些记录

V




~S


> foo.mData不会从我的内存池中获取,对吗?没有

方式来了解Foo()将分配多少内存。但是*将* mData内存放在与我的foo对象相同的池中是很好的。




如果你不打算覆盖全局新建和删除,您可以创建一个

内存分配器对象并将其传递给Foo,它将告诉它如何为其内部对象分配内存。如果写得好,分配器

可以参数化使用任意数量的不同分配

机制 - placement new,malloc,一些静态内存数组等。

然后,您将使用分配器创建对象Foo并将

分配器传递给Foo,以便它从相同的

内存存储中创建内部对象。 br />

Lyell


If I have a class Foo like this:

class Foo
{
public:
Foo()
{
mData = new char[ time() % 1000 ];
}
~Foo()
{
delete [] mData;
}
private:
char* mData;
};

Now if I create a Foo object using the ''placement'' new like so:
void* raw = malloc( sizeof(Foo()) );
Foo *foo = new(raw) Foo();

foo.mData will not be taken from my memory pool, correct? There is no
way to know that how much memory Foo() will allocate. But it *would* be
nice to have the mData memory in the same pool as my foo object.

Any tips or tricks that I am overlooking, that would allow me to do this
(without access to the Foo class)?

Thanks,
~S

解决方案

Shea Martin wrote:

If I have a class Foo like this:

class Foo
{
public:
Foo()
{
mData = new char[ time() % 1000 ];
}
~Foo()
{
delete [] mData;
}
private:
char* mData;
};

Now if I create a Foo object using the ''placement'' new like so:
void* raw = malloc( sizeof(Foo()) );
Foo *foo = new(raw) Foo();

foo.mData will not be taken from my memory pool, correct?
Which one is "your" memory pool? Essentially both ''malloc'' and ''new''
(or ''new[]'') perform allocations in what is known as "free store". For
all we know, it''s one and the same "memory pool" for both of them.
There is no
way to know that how much memory Foo() will allocate. But it *would*
be nice to have the mData memory in the same pool as my foo object.
Would it? Really? Why?
Any tips or tricks that I am overlooking, that would allow me to do
this (without access to the Foo class)?



You _could_ provide your own ''malloc'' and ''new[]''. If you know for sure
that ''Foo'' uses "new[]", then you just need to overload the function
called ''::operator new[]'' and do what you need there.

V
--
Please remove capital As from my address when replying by mail


Victor Bazarov wrote:

Which one is "your" memory pool? Essentially both ''malloc'' and ''new''
(or ''new[]'') perform allocations in what is known as "free store". For
all we know, it''s one and the same "memory pool" for both of them. Really? You have no idea what I am talking about when I say my memory
pool? While you are correct, I didn''t explicitly state that ''my memory
pool'' is a chuck of memory, somewhere other than the free store, but
most people probably connected the dots on their own. IMHO, you
probably did too, but because you are an elitist, you chose to be sarcastic.

There is no
way to know that how much memory Foo() will allocate. But it *would*
be nice to have the mData memory in the same pool as my foo object.
Would it? Really? Why?


When working with a finite amount of memory, and no virtual memory, it
helps to have a memory budget. Especially when there are different
groups working on different parts of the project. If I allocate memory,
I want only to take away from my pool, if someone in another group
allocates for something, I want their object to only use memory from
their pool. But I''ll bet you could have guessed the something like
that, but chose to be a smart arse.

Any tips or tricks that I am overlooking, that would allow me to do
this (without access to the Foo class)?
You _could_ provide your own ''malloc'' and ''new[]''. If you know for sure
that ''Foo'' uses "new[]", then you just need to overload the function
called ''::operator new[]'' and do what you need there.


Thank you. A suggestion that is worth the bytes used to store it. The
problem with overriding the default operator new and malloc, is wouldn''t
that force the application to use my new/malloc. This doesn''t really
help when have 3 groups all with their own memory budget. Although, it
does help, in that I could perform some logging

V



~S


> foo.mData will not be taken from my memory pool, correct? There is no

way to know that how much memory Foo() will allocate. But it *would* be
nice to have the mData memory in the same pool as my foo object.



If you don''t plan to override global new and delete, you can create a
memory allocator object and pass it to Foo which will tell it how to
allocate memory for it internal objects. If written well, the allocator
can be parameterized to use any number of different allocation
mechanisms - placement new, malloc, some static array of memory, etc.
You would then use the allocator to create object Foo and pass the
allocator to Foo so that it creates internal objects from the same
memory store.

Lyell


这篇关于'placement'new,以及在构造函数中的分配。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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