内存大小由new分配 [英] memory size allocated by new

查看:88
本文介绍了内存大小由new分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题之前已经发布到这个论坛,我读了

帖子,但发现答案可能不完整,所以我再次尝试



每当我创建对象时,我想跟踪用于每个对象的

内存(用于统计目的)。它们是2

基本方法,一种是写一个内存管理器类的类,其中

new&删除会超载标准的新&删除运算符。

每次调用此类的新成员函数时我都会更新变量以跟踪由
$分配的字节数b $ b新。这对我来说并不完全令人满意,因为这对应用程序来说是全局性的。


另一种方法是重新计算字节数组的大小并添加

成员变量的结果。这样的事情...


A级

{

私人:

int * data ;

float * floatData;

size_t mem;

public:

A(size_t arraySize,size_t arraySize2)

{

mem = 0;

data = new int [arraySize];

dataFloat = new float [ arraySize2]

mem + = sizeof(int)* arraySize;

mem + = sizeof(float)* arraySize2;

}

}


我读过''new''保留某种类型的

数组所需的内存,再加上一些额外的信息在开始时

这个数组(删除用来知道它需要释放的数组大小是多少)。因此,不是以字节为单位重新计算数组的大小

并将结果总结为变量mem有一种方法可以使用
以字节为单位访问数组的大小由数组使用或由

new创建?


谢谢 -

This question has been posted to this forum before and I read the
thread but found that the answers were perhaps imcomplete, so I am
trying again.

Whenever I am creating objects I would like to keep track of the
memory which is used for each object (for stats purpose). They are 2
basic approaches, one is to write a Memory manager type of class where
new & delete would be overloading the standard new & delete operator.
Each time the new member function of this class would be called I
could update a variable to would keep track of the bytes allocated by
new. This not completly satisafactory for me as this would be global
to the application.

Another way is to just recompute the size of the byte arrays and add
the result to a member variable. Something like that...

class A
{
private:
int *data;
float *floatData;
size_t mem;
public:
A( size_t arraySize, size_t arraySize2 )
{
mem = 0;
data = new int[ arraySize ];
dataFloat = new float[ arraySize2 ]
mem += sizeof( int ) * arraySize;
mem += sizeof( float ) * arraySize2;
}
}

I have read that ''new'' reserves the memory which is needed for the
array of a certain type, plus some additional info at the beginning at
this array (which delete uses to know what is the size of the array it
needs to free). So instead of recompute the size of the array in bytes
and summing up the result to the variable mem is there a way one could
access the size of an array in bytes used by an array or created by
new ?

thanks -

推荐答案

3月6日下午7:59,mast ... @ yahoo.com < mast ... @ yahoo.comwrote:
On Mar 6, 7:59 pm, "mast...@yahoo.com" <mast...@yahoo.comwrote:

此问题已经发布到此论坛之前我读过

帖子但是发现答案可能不完整,所以我再次尝试



每当我创建对象时,我想跟踪

用于每个对象的内存(用于统计目的)。它们是2

基本方法,一种是写一个内存管理器类的类,其中

new&删除会超载标准的新&删除运算符。

每次调用此类的新成员函数时我都会更新变量以跟踪由
$分配的字节数b $ b新。这对我来说并不完全令人满意,因为这对应用程序来说是全球性的。
This question has been posted to this forum before and I read the
thread but found that the answers were perhaps imcomplete, so I am
trying again.

Whenever I am creating objects I would like to keep track of the
memory which is used for each object (for stats purpose). They are 2
basic approaches, one is to write a Memory manager type of class where
new & delete would be overloading the standard new & delete operator.
Each time the new member function of this class would be called I
could update a variable to would keep track of the bytes allocated by
new. This not completly satisafactory for me as this would be global
to the application.



对不起,我不理解你。您是否要求某种方式跟踪全局分配的字节数?
或者仅在

某些位置?

Sorry, I''m not understanding you. Are you asking for some way of
tracking how many bytes are being allocated globally? Or only in
certain spots?


另一种方法是重新计算字节数组的大小并添加

结果为成员变量。这样的事情...


A级

{

私人:

int * data ;

float * floatData;

size_t mem;

public:

A(size_t arraySize,size_t arraySize2)

{

mem = 0;

data = new int [arraySize];

dataFloat = new float [ arraySize2]

mem + = sizeof(int)* arraySize;

mem + = sizeof(float)* arraySize2;

}


}
Another way is to just recompute the size of the byte arrays and add
the result to a member variable. Something like that...

class A
{
private:
int *data;
float *floatData;
size_t mem;
public:
A( size_t arraySize, size_t arraySize2 )
{
mem = 0;
data = new int[ arraySize ];
dataFloat = new float[ arraySize2 ]
mem += sizeof( int ) * arraySize;
mem += sizeof( float ) * arraySize2;
}

}



我也没有得到这个。你正在分配数组,并试图将
存储到数组的大小,但你只想在类中完成它?

I''m not getting this either. You are allocating arrays, and trying to
store the size of the arrays, but you only want it done in a class?


I已经读过''new''保留某种类型的

数组所需的内存,加上一些额外的信息,开头就是

这个数组(其中delete用来知道它需要释放的数组的大小是多少。因此,不是以字节为单位重新计算数组的大小

并将结果总结为变量mem有一种方法可以使用
以字节为单位访问数组的大小由数组使用或由

new创建?
I have read that ''new'' reserves the memory which is needed for the
array of a certain type, plus some additional info at the beginning at
this array (which delete uses to know what is the size of the array it
needs to free). So instead of recompute the size of the array in bytes
and summing up the result to the variable mem is there a way one could
access the size of an array in bytes used by an array or created by
new ?



好​​的,我得到了。一个数组将是sizeof(type)* numberOfElements但是如果

类型有一个析构函数,那么你必须将sizeof(size_t)添加到

它。


如果你只是想知道全局分配了多少字节,

这样做:


< sourceFile>

静态size_t bytesAllocatedSoFar = 0;


void * :: operator new(size_t size)

{

bytesAllocatedSoFar + = size;

返回malloc(大小);

}


void * :: operator new [ ](size_t大小)

{

bytesAllocatedSoFar + = size;

返回malloc(大小);

}


void :: operator delete(void * memory)

{

返回免费(内存);

}


void :: operator delete [](void * memory)

{

返回免费(内存) ;

}

< / sourceFile>


但是如果程序的任何部分使用malloc,则此信息为

丢失。


如果你想访问bytesAllocatedSoFar,你将在头文件中定义它

extern(并从源文件中删除静态),或者

在头文件中创建一个访问函数原型并在源代码中定义它作为返回bytesAllocatedSoFar的



希望这个帮助。

Adrian

Ok, this I get. An array will be sizeof(type)*numberOfElements but if
the type has a destructor, then you will have to add sizeof(size_t) to
it.

If you just want to know how many bytes are being allocated globally,
do this:

<sourceFile>
static size_t bytesAllocatedSoFar = 0;

void* ::operator new(size_t size)
{
bytesAllocatedSoFar += size;
return malloc(size);
}

void* ::operator new[](size_t size)
{
bytesAllocatedSoFar += size;
return malloc(size);
}

void ::operator delete(void *memory)
{
return free(memory);
}

void ::operator delete[](void *memory)
{
return free(memory);
}
</sourceFile>

But if any part of your programme uses malloc, this information is
lost.

If you want to access bytesAllocatedSoFar you will either define it
extern in the header file (and remove static from the source file), or
create an access function prototype in the header file and define it
in the source as returning bytesAllocatedSoFar.

Hope this helps.
Adrian


ma *****@yahoo.com 写道:

此问题已经发布到此论坛之前我读过

线程但发现答案可能不完整,所以我再次尝试



每当我创建对象时,我想跟踪

内存用于每个对象(用于统计目的)。它们是2

基本方法,一种是写一个内存管理器类的类,其中

new&删除会超载标准的新&删除运算符。

每次调用此类的新成员函数时我都会更新变量以跟踪由
$分配的字节数b $ b新。这对我来说并不完全令人满意,因为这对应用程序来说是全局性的。


另一种方法是重新计算字节数组的大小并添加

成员变量的结果。这样的事情...


A级

{

私人:

int * data ;

float * floatData;

size_t mem;

public:

A(size_t arraySize,size_t arraySize2)

{

mem = 0;

data = new int [arraySize];

dataFloat = new float [ arraySize2]

mem + = sizeof(int)* arraySize;

mem + = sizeof(float)* arraySize2;

}

}


我读过''new''保留某种类型的

数组所需的内存,再加上一些额外的信息在开始时

这个数组(删除用来知道它需要释放的数组大小是多少)。因此,不是以字节为单位重新计算数组的大小

并将结果总结为变量mem有一种方法可以使用
以字节为单位访问数组的大小由数组使用或由

新创建?


谢谢 -
This question has been posted to this forum before and I read the
thread but found that the answers were perhaps imcomplete, so I am
trying again.

Whenever I am creating objects I would like to keep track of the
memory which is used for each object (for stats purpose). They are 2
basic approaches, one is to write a Memory manager type of class where
new & delete would be overloading the standard new & delete operator.
Each time the new member function of this class would be called I
could update a variable to would keep track of the bytes allocated by
new. This not completly satisafactory for me as this would be global
to the application.

Another way is to just recompute the size of the byte arrays and add
the result to a member variable. Something like that...

class A
{
private:
int *data;
float *floatData;
size_t mem;
public:
A( size_t arraySize, size_t arraySize2 )
{
mem = 0;
data = new int[ arraySize ];
dataFloat = new float[ arraySize2 ]
mem += sizeof( int ) * arraySize;
mem += sizeof( float ) * arraySize2;
}
}

I have read that ''new'' reserves the memory which is needed for the
array of a certain type, plus some additional info at the beginning at
this array (which delete uses to know what is the size of the array it
needs to free). So instead of recompute the size of the array in bytes
and summing up the result to the variable mem is there a way one could
access the size of an array in bytes used by an array or created by
new ?

thanks -



你的课程是有缺陷和邋..我会这样写的:


A级{

私人:

std :: vector< intdata;

std :: vector< floatfloatData;

public:

A(size_t arraySize,size_t arraySize2):

data (arraySize),floatData(arraySize2){}

size_t approximate_size(){

返回data.size()* sizeof(int)+

floatData.size()* sizeof(float);

}

};


通过使用vector,你自动上课在副本中明确定义

和作业。此外,它让您不必担心内存分配

。您也可以轻松更改大小。


approximate_size函数返回你的mem

变量所具有的东西,但只需要计算使用时(并且

根据向量的大小调整是正确的)。


Your class is defective and sloppy. I''d write it like this:

class A {
private:
std::vector<intdata;
std::vector<floatfloatData;
public:
A(size_t arraySize, size_t arraySize2) :
data(arraySize), floatData(arraySize2) { }
size_t approximate_size() {
return data.size() * sizeof(int) +
floatData.size() * sizeof(float);
}
};

By using vector, you class automatically becomes well defined in copy
and assignment. Further it frees you from having to worry about
memory allocation. You can easily change the size as well.

The approximate_size function returns the smae thing your "mem"
variable would have, but only needs be calculated when used (and
is correct in light of resizing of the vectors).



>

对不起,我不理解你。您是否要求某种方式跟踪全局分配的字节数?
或者只在

某些地方?
>
Sorry, I''m not understanding you. Are you asking for some way of
tracking how many bytes are being allocated globally? Or only in
certain spots?



对不起阿德里安我不清楚。我只对A类对象使用的内存感兴趣

。例如,如果它们是类的100个对象

A,它们都是为floatData创建的,具有不同的大小&安培;数据,我想知道这100个对象如何使用内存。这就是为什么我想要避免使用某种类型的内存管理器类,而这些内存管理器类需要新的和删除重载。


我的问题也是关于这些额外的位,新运算符

似乎控制保存有关它创建的字节数组大小的信息。我想知道是否可以以某种方式访问​​该信息,所以

可以从那里获得该值!

Sorry Adrian I wasn''t clear. I am only interested in the memory used
by objects of class A. So for example if they are 100 objects of class
A which are created all with different sizes for floatData & data, I
want to know how memory is used by these 100 objects. This is why I
wanted to avoid using some sort of memory manager class that would
have new and delete overloaded.

My question was also about these extra bits that the new operator
seems to control to save info about the size of the byte arrays it has
created. I was wondering if that info could be accessed somehow, so
one could get that value from there !


这篇关于内存大小由new分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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