分配给指针的内存量 [英] amount of memory allocated to a pointer

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

问题描述

我怎么能知道有多少内存被阻止(或已被分配

指针)


考虑,

int * p = new int;



int * p = new int [100];


假设我不知道语句的右侧即

new int或new int [100]或new int [n](其中n是在

运行时期间计算的)


(绝对sizeof(p)不会给我分配的内存金额

。)


i会有兴趣了解程序中各个指针所占用的内存量。我怎么可能实现这个?

有没有任何方法或标准例程来衡量内存的数量?

如果没有标准方法或惯例为什么会如此?

解决方案

pratap写道:


我怎么能知道有多少内存被阻止(或已被分配

指针)


考虑,

int * p = new int;



int * p = new int [100];



这对我来说看起来不像。你的意思是发布到comp.lang.c ++?


假设我不知道语句的右侧即

new int或新的int [100]或new int [n](其中n是在
运行时期间计算的)


(绝对sizeof(p)不会给我分配的内存量

。)


i有兴趣知道

各个指针占用的内存量在一个程序中。我怎么可能实现这个?

有没有任何方法或标准程序来衡量内存的数量?



不在C中,我不相信C ++有任何不同。


如果有没有标准的方法或惯例为什么会这样?



我有一位前同事曾经说他会回答什么和

怎么样问题,但不是为什么问题......


基本上,这种语言根本不会那样工作。


Mark Bluemel说:


pratap写道:



< snip>

< blockquote class =post_quotes>
>我有兴趣知道程序中各个指针占用的内存量。我怎么可能实现这个?
有没有任何方法或标准程序来衡量内存的数量?



不在C中,我不相信C ++有任何不同。



实际上,指针占用的内存量很容易确定。在引用的段落(上文)中,OP询问错误的

问题。他应该问的是,给定一个指向

动态分配内存的指针,有多少正确类型的对象可以存储在这样分配的内存中。答案非常简单:

当一个人首先分配这个内存时,一个人知道可以在其中存储多少个b $ b对象,所以所有人必须做的就是不要忘了。


< snip>


-

Richard Heathfield< http:// www .cpax.org.uk>

电子邮件:-www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日


9月12日下午2:50,Richard Heathfield< r ... @ see.sig.invalidwrote:


Mark Bluemel说:


pratap写道:



< ; snip>


i有兴趣了解

各个指针占用的内存量在一个程序中。我怎么可能实现这个?

有没有任何方法或标准程序来衡量内存的数量?


不在C中,我不相信C ++有任何不同。



实际上,指针占用的内存量很容易确定。在引用的段落(上文)中,OP询问错误的

问题。他应该问的是,给定一个指向

动态分配内存的指针,有多少正确类型的对象可以存储在这样分配的内存中。答案非常简单:

当一个人首先分配这个内存时,一个人知道可以在其中存储多少个b $ b对象,所以所有人必须做的就是不要忘了。


< snip>


-

Richard Heathfield< http:// www .cpax.org.uk>

电子邮件:-www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - - 1999年7月29日



使用_msize(p)的另一种方式

这比记住更容易! ! - )


how could i find out how much memory is blocked(or has been allocated
to a pointer)

consider,
int *p=new int;
or
int *p=new int[100];

suppose i dont know the right hand side of the statement i.e.
new int or new int[100] or new int[n] (where n is calculated during
runtime)

(definitely sizeof(p) would not give me the amount of memory
allocated.)

i would be interested in knowing the amount of memory taken up by the
respective pointers in a program. how could i possibly achieve this?
Are there any methods or standard routines to measure the amount of
memory?
If there are no standard methods or routines why would this be so ?

解决方案

pratap wrote:

how could i find out how much memory is blocked(or has been allocated
to a pointer)

consider,
int *p=new int;
or
int *p=new int[100];

This doesn''t look like C to me. Did you mean to post to comp.lang.c++?

suppose i dont know the right hand side of the statement i.e.
new int or new int[100] or new int[n] (where n is calculated during
runtime)

(definitely sizeof(p) would not give me the amount of memory
allocated.)

i would be interested in knowing the amount of memory taken up by the
respective pointers in a program. how could i possibly achieve this?
Are there any methods or standard routines to measure the amount of
memory?

Not in C and I don''t believe C++ is any different.

If there are no standard methods or routines why would this be so ?

I had a former colleague who used to say that he would answer "what" and
"how" questions, but not "why" questions...

Basically, the language simply doesn''t work that way.


Mark Bluemel said:

pratap wrote:

<snip>

>i would be interested in knowing the amount of memory taken up by the
respective pointers in a program. how could i possibly achieve this?
Are there any methods or standard routines to measure the amount of
memory?


Not in C and I don''t believe C++ is any different.

Actually, the amount of memory taken up by a pointer is very easy to
determine. In the quoted paragraph (above) the OP asks the wrong
question. What he ought to be asking is how, given a pointer to
dynamically allocated memory, how many objects of the proper type can
be stored in the memory thus allocated. And the answer is very easy:
when one allocates this memory in the first place, one knows how many
objects can be stored therein, so all one has to do is Not Forget.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


On Sep 12, 2:50 pm, Richard Heathfield <r...@see.sig.invalidwrote:

Mark Bluemel said:

pratap wrote:


<snip>

i would be interested in knowing the amount of memory taken up by the
respective pointers in a program. how could i possibly achieve this?
Are there any methods or standard routines to measure the amount of
memory?

Not in C and I don''t believe C++ is any different.


Actually, the amount of memory taken up by a pointer is very easy to
determine. In the quoted paragraph (above) the OP asks the wrong
question. What he ought to be asking is how, given a pointer to
dynamically allocated memory, how many objects of the proper type can
be stored in the memory thus allocated. And the answer is very easy:
when one allocates this memory in the first place, one knows how many
objects can be stored therein, so all one has to do is Not Forget.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

one more way is using _msize(p)
this is even more easier than remembering! !-)


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

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