为给定指针分配的内存。 [英] Memory allocated for a given Pointer.

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

问题描述

大家好,

有没有什么方法可以找出给指针给

指定的内存量。


例如:

int * p = new int [10];


我知道,编译器会保留分配了多少字节的信息

for

指针p所以它可以在删除时发布。


但我们可以通过任何方式得到这些信息吗?

谢谢

Sabiyur

Hi All,
Is there any way to find out how much memory is allocated for a
given pointer.

For example:
int *p=new int[10];

I know, compiler keeps the information of how much bytes are allocated
for
the pointer "p" so that it can be released on delete.

But can we get this info by any way?
Thanks
Sabiyur

推荐答案



Sabiyur写道:

Sabiyur wrote:

大家好,

有没有办法知道给定指针给

分配了多少内存。


For例如:

int * p = new int [10];


我知道,编译器会保留分配多少字节的信息

for

指针p所以它可以在删除时发布。


但我们可以通过任何方式得到这些信息吗?
Hi All,
Is there any way to find out how much memory is allocated for a
given pointer.

For example:
int *p=new int[10];

I know, compiler keeps the information of how much bytes are allocated
for
the pointer "p" so that it can be released on delete.

But can we get this info by any way?

http://www.parashift.com/c++-faq-lit...html#faq-16.14


更重要的是,请参阅:

http://www.parashift。 com / c ++ - faq-lit .... html#faq-34.1


干杯! --M

http://www.parashift.com/c++-faq-lit...html#faq-16.14

More importantly, see:

http://www.parashift.com/c++-faq-lit....html#faq-34.1

Cheers! --M


Sabiyur写道:
Sabiyur wrote:

int * p = new int [10] ;


我知道,编译器会保留指定多少字节的信息

指针" p"所以它可以在删除时发布。


但我们可以通过任何方式得到这些信息吗?
int *p=new int[10];

I know, compiler keeps the information of how much bytes are allocated for
the pointer "p" so that it can be released on delete.

But can we get this info by any way?



这可能是常见问题解答。标准C ++库无法使用。

暗示当你分配数组时,你不应该丢弃信息。


使用标准: :适用于几乎任何使用原始数组的情况。

首先学习矢量,然后它将为你存储数组大小。


如果您处于紧张状态,_msize()或类似可能会起作用,

特定于平台。在代表

平台的论坛上进一步询问此类问题。


-

Phlip

This is probably a FAQ. The Standard C++ Library has no way. The
implication is you shouldn''t throw the information away when you allocate
the array.

Use a std::vector for nearly any situation where you''d use a raw array.
Learn the vector first, and it will store its array size for you.

If you are in a pinch, _msize() or similar might work,
platform-specifically. Ask further such questions on a forum representing
your platform.

--
Phlip


Sabiyur发布:
Sabiyur posted:

大家好,

有什么办法可以找出多少内存分配给指定的
指针。
Hi All,
Is there any way to find out how much memory is allocated for a
given pointer.



#include< cstddef>


int main()

{

int * p;


std :: size_t amount_mem = sizeof p;

}


#include <cstddef>

int main()
{
int *p;

std::size_t amount_mem = sizeof p;
}


例如:

int * p = new int [10];


我知道,编译器保存信息分配了多少字节



指针p所以它可以在删除时发布。


但我们可以通过任何方式得到这些信息吗?
For example:
int *p=new int[10];

I know, compiler keeps the information of how much bytes are allocated
for
the pointer "p" so that it can be released on delete.

But can we get this info by any way?



Hi-jack" malloc"和新的写你自己的。将所有地址

存储在某种查找表中。


-


Frederick Gotham


Hi-jack "malloc" and "new" by writing your own. Store all the addresses
in some sort of look-up table.

--

Frederick Gotham


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

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