如何计算malloc返回的缓冲区大小 [英] how to figure out the size of buffer returned by malloc

查看:104
本文介绍了如何计算malloc返回的缓冲区大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果我有以下内容:


char * p = malloc(5);

memset( p,-1,5);

* p = 0;

printf("%d \ nn",strlen(p));

免费(p);


它将打印0.有没有办法重新检索内存的初始大小

由malloc返回?除了使用额外的变量来保持

的初始大小,例如:


size_t i = 5;

char * p = malloc(i);

....


然后将初始请求的大小存储在i中。是否有一个

结束的malloc-marker?


谢谢!

Hi,
If I have the folllowing:

char* p = malloc(5);
memset(p,-1,5);
*p = 0;
printf("%d\n",strlen(p));
free(p);

It will print 0. Is there a way to retrive the initial size of memory
returned by malloc? Other than using an extra variable to hold the
initial size like:

size_t i = 5;
char* p = malloc(i);
....

The initial requested size is then stored in i. Is there a
end-of-malloc-marker?

Thanks!

推荐答案

pe********@yahoo.com (pembed2003)写道/>
新闻:db ************************** @ posting.google.c om:
pe********@yahoo.com (pembed2003) wrote in
news:db**************************@posting.google.c om:

如果我有以下内容:

char * p = malloc(5);
memset(p,-1,5);
* p = 0;
printf("%d \ nn",strlen(p));
free(p);

它将打印0。有没有办法恢复malloc返回的内存初始大小?除了使用额外的变量来保持
初始大小,例如:

size_t i = 5;
char * p = malloc(i);


否。您必须自己跟踪。

然后将初始请求的大小存储在i中。是否有一个
结束malloc标记?
Hi,
If I have the folllowing:

char* p = malloc(5);
memset(p,-1,5);
*p = 0;
printf("%d\n",strlen(p));
free(p);

It will print 0. Is there a way to retrive the initial size of memory
returned by malloc? Other than using an extra variable to hold the
initial size like:

size_t i = 5;
char* p = malloc(i);
No. You must track it yourself.
The initial requested size is then stored in i. Is there a
end-of-malloc-marker?




不是C知道的。


- -

- 马克 - >

-



Not that C knows about.

--
- Mark ->
--




" pembed2003" ; < PE ******** @ yahoo.com>在留言中写道

news:db ************************** @ posting.google.c om ...

"pembed2003" <pe********@yahoo.com> wrote in message
news:db**************************@posting.google.c om...

如果我有以下内容:

char * p = malloc(5);
memset(p,-1,5) ;
* p = 0;
printf("%d \ nn",strlen(p));


应该是:


printf("%lu \ n",(unsigned long)strlen(p));

免费(p);

它会打印0.有没有办法重新检索malloc返回的内存初始大小?


''malloc()''不会返回它分配的大小,

但指向它的指针。

唯一的保证是''malloc()''(如果成功,

由非NULL返回值表示),将至少分配*

请求的字节数。它允许分配更多(但是

你不能访问超过你的请求)。

除了使用额外的变量来保持
初始值大小如:

size_t i = 5;
char * p = malloc(i);
...

初始请求的大小是存储在i。是否有一个
结束malloc标记?
Hi,
If I have the folllowing:

char* p = malloc(5);
memset(p,-1,5);
*p = 0;
printf("%d\n",strlen(p));
Should be:

printf("%lu\n", (unsigned long)strlen(p));
free(p);

It will print 0. Is there a way to retrive the initial size of memory
returned by malloc?
''malloc()'' does not return the size of what it allocates,
but a pointer to it.

The only guarantee you have is that ''malloc()'' (if it succeeds,
indicated by non-NULL return value), will allocate *at least* the
number of bytes requested. It''s allowed to allocate more (but
you must not access more than you requested).
Other than using an extra variable to hold the
initial size like:

size_t i = 5;
char* p = malloc(i);
...

The initial requested size is then stored in i. Is there a
end-of-malloc-marker?




编号但你可以通过添加一个来轻松地模拟一个

字节到您要求的大小,并在那里存储一些特殊的''sentinel''

值,例如:


size_t size = 5;

char marker(0xFF);

char * p = malloc(size + 1);

*(p + size)= marker;


你想做什么?


-Mike



No. But you can easily ''simulate'' one by adding one more
byte to your requested size, and storing some special ''sentinel''
value there, e.g.:

size_t size = 5;
char marker(0xFF);
char *p = malloc(size + 1);
*(p + size) = marker;

What are you trying to do?

-Mike


pembed2003写道:
pembed2003 wrote:

如果我有以下内容:

char * p = malloc(5);
memset(p,-1,5) );
* p = 0;
printf("%d \ nn",strlen(p));
免费(p);

它将打印0.有没有办法恢复malloc返回的内存的初始大小?除了使用额外的变量来保持
初始大小,如:

size_t i = 5;
char * p = malloc(i);
...

然后将初始请求的大小存储在i中。是否有一个
结束malloc标记?
Hi,
If I have the folllowing:

char* p = malloc(5);
memset(p,-1,5);
*p = 0;
printf("%d\n",strlen(p));
free(p);

It will print 0. Is there a way to retrive the initial size of memory
returned by malloc? Other than using an extra variable to hold the
initial size like:

size_t i = 5;
char* p = malloc(i);
...

The initial requested size is then stored in i. Is there a
end-of-malloc-marker?




这是comp.lang.c中的问题7.27经常

问答(FAQ)列表

http://www.eskimo.com/~scs/C-faq/top.html


另见问题7.26。

-
Er ********* @ sun.com



This is Question 7.27 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

See also Question 7.26.

--
Er*********@sun.com


这篇关于如何计算malloc返回的缓冲区大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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