查找分配的堆大小 [英] find size of heap allocated

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

问题描述

我想为以下语句查找在堆上分配的总内存大小:

I want to find the size of the total memory allocated on the heap for the following statement:

int* a = new int[1000];

如何为此使用sizeof运算符?
我用过:

How can I use the sizeof operator for this?
I used:

printf("\t===> %d\n",sizeof(*a));



这句话正确吗?

我问这个问题是因为当我检查在windbg中分配的堆内存时,它显示的大小为fc4,即4036,大于4000,而不是所需的大小.



Is this statement correct?

I have asked the question because when I checked the memory of heap allocated in windbg it shows me the size as fc4 i.e. 4036 which is more then 4000 and not as desired. Any hint as to what may be the cause?

推荐答案

不,您不能那样得到,那会给您一个<a指向的c0>.

在您的示例中,您将必须执行sizeof(int) * 1000来获取分配的数量,因为您持有的所有引用都是指针,而不是数组.要获取数组分配的数量,必须将其声明为数组
(例如int myArray[1000];).

考虑以下三个示例:

No, you can''t get it like that, that''ll give you the size of an int which is what a is pointing to.

In your example you''ll have to do sizeof(int) * 1000 to get the allocated amount, because all you hold a reference to is the pointer, not an array. To get the amount allocated by the array it has to be declared as an array
(for example int myArray[1000];).

Consider the following three examples:

int* a = new int[1000];
int b[1000];
char c[] = "Hello, world!";

cout << "sizeof(int) * 1000 = " << (sizeof(int)*1000) << endl;
cout << "sizeof a  = " << (sizeof a) << endl;
cout << "sizeof *a  = " << (sizeof *a) << endl;

cout << "sizeof b  = " << (sizeof b) << endl;
cout << "sizeof *b  = " << (sizeof *b) << endl;

cout << "sizeof c  = " << (sizeof c) << endl;
cout << "sizeof *c  = " << (sizeof *c) << endl;



它将打印出类似以下内容的内容:



Which will print something like:

sizeof(int) * 1000 = 4000
sizeof a  = 4
sizeof *a  = 4
sizeof b  = 4000
sizeof *b  = 4
sizeof c  = 14
sizeof *c  = 1



希望这会有所帮助,
弗雷德里克(Fredrik)



Hope this helps,
Fredrik



sizeof(*a)*1000


将为您分配以字节为单位的总内存.


Will give you the total memory allocated in bytes.


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

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