malloc大小 [英] malloc size
问题描述
在程序中如何找出使用malloc分配给
指针的大小。例如...
int * p;
p =(int *)malloc(1024 * sizeof(int));
现在怎么找到分配到p的多少?
mojozoox!
< blockquote> ma*******@HotPop.com 写道:
里面一个程序如何找出使用malloc分配给指针的大小。例如......
int * p =(int *)malloc(1024 * sizeof(int));
现在如何找到分配给p的多少?
如果malloc成功,结果将具有您要求的大小(在此
的情况下,1024 * sizeof(int))。如果你正在传递指针并且不知道
知道有多少是malloc''ed,你只需要跟踪指针信息
。这可能看起来很愚蠢,因为
底层内存分配系统几乎总是需要跟踪这个
信息本身,但是可以想象分配系统
只会隐含地保留它并使其难以获得。
-
Derrick Coetzee
我将此新闻组发布到公共领域。我不承担所有
明示或暗示保证并承担所有责任。我不是专业人士。
您好
AFAIK,它无法找到malloc分配的数组大小。
你向malloc提供这个值...所以,你是那个必须保持跟踪它的人。
通常,一个常数说#define ARRAYSIZE 1024使用了这个
常量用于malloc和其他任何需要的数组大小的元素
发现
HTH
Anitha
ma ******* @ HotPop.com 写道:
在程序中如何找出使用malloc分配给指针的大小。例如...
int * p;
p =(int *)malloc(1024 * sizeof(int));
现在如何找到已分配多少p?
mojozoox!
2005年1月5日19:29:18 -0800, ma ******* @ HotPop.com 写道:
在程序中如何找出使用malloc分配给指针的大小。例如...
int * p;
p =(int *)malloc(1024 * sizeof(int));
现在如何找到已分配多少p?
如果你自己没有跟踪它,就没有便携方式。
<<删除电子邮件的del> >
Hi,
Inside a program how do i find out what size has been allocated to
pointer using malloc. eg...
int *p;
p=(int*)malloc(1024*sizeof(int));
now how do find how much has been allocated to p?
mojozoox!
ma*******@HotPop.com wrote:Inside a program how do i find out what size has been allocated to
pointer using malloc. eg...
int *p=(int*)malloc(1024*sizeof(int));
now how do find how much has been allocated to p?
If malloc succeeds, the result will have the size you asked for (in this
case, 1024*sizeof(int)). If you''re passing pointers around and don''t
know how much was malloc''ed, you simply have to track that information
yourself along with the pointer. This may seem silly, since the
underlying memory allocation system almost always needs to track this
information itself, but there are conceivably allocation systems that
would keep it only implicitly and make it hard to obtain.
--
Derrick Coetzee
I grant this newsgroup posting into the public domain. I disclaim all
express or implied warranty and all liability. I am not a professional.
Hi
AFAIK, its not possible to find the size of array allocated by malloc.
You provide this value to malloc...so, you are the one who has to keep
track of it.
usually, a constant say "#define ARRAYSIZE 1024" is used and this
constant is used in malloc and anywhere else the array size needs to be
found
HTH
Anitha
ma*******@HotPop.com wrote:Hi,
Inside a program how do i find out what size has been allocated to
pointer using malloc. eg...
int *p;
p=(int*)malloc(1024*sizeof(int));
now how do find how much has been allocated to p?
mojozoox!
On 5 Jan 2005 19:29:18 -0800, ma*******@HotPop.com wrote:
Hi,
Inside a program how do i find out what size has been allocated to
pointer using malloc. eg...
int *p;
p=(int*)malloc(1024*sizeof(int));
now how do find how much has been allocated to p?
If you don''t keep track of it yourself, there is no portable way.
<<Remove the del for email>>
这篇关于malloc大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!